Quantcast
Channel: Implement division using only addition - Code Golf Stack Exchange
Browsing latest articles
Browse All 18 View Live

Answer by marinus for Implement division using only addition

J, 0 additions, 14 bytesInspired by Alexei Kopylov's answer.f=:[{]#i.@>:@[Uses no maths at all:f=: NB. define function f [ NB. take left argument,>:@ NB. increment it, i.@ NB. generate the list...

View Article



Answer by Todd Lehman for Implement division using only addition

C — 85591 AdditionsHere we go. I think this might be optimal. It uses a technique of "reverse division" whereby through long multiplication it builds up the largest number q such that q * b <= a,...

View Article

Answer by Alexei Kopylov for Implement division using only addition

Haskell 0 additions, 29 bytesn/m=[i|i<-[0..],_<-[1..m]]!!nthis redefines the division operator (/). it works by making a list of 0 to infinity where each item is repeated m times, and then...

View Article

Answer by Joop Eggen for Implement division using only addition

Java: 92 987 additionsI use binary recursion, that a/b == 2 * a/(2b) + maybe 1. For that divisor and remainder are needed. There would normally be a subtraction a % (2b) - b, but that is resolved by...

View Article

Answer by Cary Swoveland for Implement division using only addition

In Ruby,def divide(a,b) n, d = 'x' * a, 'x' * b l = [] (l << 'x'; d << 'x' * b) while n.size >= d.size l.sizeend I don't know TCL, but I suspect this is the same approach as @Johannes '...

View Article


Answer by ST3 for Implement division using only addition

My solution is C/C++ code and it makes many additions (200402), but anyway...#include <iostream>int total = 0;int sum(int a, int b){++total; return a + b;}int divide(int a, int b){ int x = 1; if...

View Article

Answer by Igby Largeman for Implement division using only addition

C# - 0 additionsusing System.Collections.Generic;using System.Linq;static int Divide(int a, int b){ var ints = new List<int>(a); while (ints.Count < a) ints.AddRange(Enumerable.Range(1, b));...

View Article

Answer by gnibbler for Implement division using only addition

Python - 0 additionsfrom itertools import repeat, countdef divide(a, b): i = repeat(0, a) try: for j in count(): for k in repeat(0, b): next(i) except: return jThis uses an iterator of length a, and...

View Article


Answer by plannapus for Implement division using only addition

R - 0 additiondivide<-function(a,b){ options(warn=-1) A<-matrix(1:b,nrow=a,ncol=1) length(split(A,A)[[b]]) }Uses R vector recycling.Second line creates a matrix of length a populated by a vector...

View Article


Answer by Dinesh Kumar P for Implement division using only addition

//a lies between 1 and 200, b lies between 1 and a.int divide(int a,int b){int x=a,y=b;int count=1;while(y<x){y+=y;count++;}return count;}

View Article

Answer by Parag for Implement division using only addition

C++ ,100201for(int a = 1; a<=200; a++){ for(int b=1;b<=a;b++){ iter1 = iter2 = b; cout<<a<<""<<b<<endl; c1 =0;while(iter1 <= a){ iter1 = iter1 + iter2; c1 ++;...

View Article

Answer by Anurag for Implement division using only addition

Use this implementation in java, 199206 additionspublic int divide(int a, int b){ int counter = 0; int c = 0; if(b==1){ return a; } if(a==b){ return 1; } else{ boolean done = false; while(!done){ c =...

View Article

Answer by DavidC for Implement division using only addition

Mathematica 100201 additionsThis adds the divisor, b, to c (which is initialized at 0) as long as the running total is less than or equal to the dividend, a. It also appends the current value of c to a...

View Article


Answer by aaaaaaaaaaaa for Implement division using only addition

Writing rules is hard, these rules in particular contain incentive to avoid additions at all costs.Is there a prize for the most ridiculous answer?JavaScript - 0 additionsNow with fallback method that...

View Article

Answer by Johannes Kuhn for Implement division using only addition

Tcl, 0 additions.Well, I had to find a way that does not use other data structures but is still not what you want:# coroutine counter.proc ccnt {} {yield [info level]; ccnt}# add implementation without...

View Article


Answer by Johannes Kuhn for Implement division using only addition

Tcl, 0 additionsproc divide {a b} { set sa [string repeat . $a] set sb [string repeat . $b] set sr "" while 1 { append sc $sb if {[string le $sc]>[string le $sa]} break append sr . } return [string...

View Article

Answer by Joe Z. for Implement division using only addition

Python, 320703 additionsdef divide(a, b): quotient = 0 c = 0 d = 0 while add(d, b) <= a: c = add(c, 1) d = add(d, b) return cAs always, a last-place reference answer. This simply adds 1 to a...

View Article


Implement division using only addition

There is a question on the site that asks to implement division without using division.In my case, I am asking you to do the same, but only using addition.What this means is basically: addition is the...

View Article
Browsing latest articles
Browse All 18 View Live




Latest Images