r/dailyprogrammer • • Sep 15 '12
[9/15/2012] Challenge #98 [intermediate] (Multiple cycling)
Write a function that takes two arguments: a limit, lim, and a list of integers, x. The function counts up from 0 by cycling through x and skipping numbers until we find the next number that's a multiple of x[i]. For example, when x is the list [5, 7, 3], start counting from 0:
- Next multiple of 5 is 5
- Next multiple of 7 is 7
- Next multiple of 3 is 9
- Next multiple of 5 is 10
- Next multiple of 7 is 14
- Next multiple of 3 is 15
When the count reaches lim or a number above it, return the number of steps it took to reach it. (multiple_cycle(15, [5, 7, 3]) would return 6.)
What is the result of multiple_count(1000000000, [5395, 7168, 2367, 9999, 3])?
11
Upvotes
5
u/Ledrug 0 2 Sep 15 '12 edited Sep 16 '12
Haskell, prints 408040
1
u/bschlief 0 0 Sep 17 '12
Ruby, with benchmarking. It is REALLY slow (~157 seconds). I've looked at some of the solutions that are speeding up computation, but I'm not understanding what's happening. Can some kind Rubyist shed some light on how to speed this up?
Result is 408040
2
u/skeeto -9 8 Sep 15 '12
In Common Lisp, destructively using a circular list.
Usage:
2
u/prondose 0 0 Sep 15 '12 edited Sep 15 '12
in Perl:
multiple_count(15, 5, 7, 3) yields 6
multiple_count(1000000000, 5395, 7168, 2367, 9999, 3) yields 408040
2
u/Ledrug 0 2 Sep 15 '12 edited Sep 16 '12
C.
2
u/Sturmi12 Sep 15 '12
Java solution, my result is 408040
1
u/lawlrng 0 1 Sep 15 '12 edited Sep 17 '12
Python 3. Assumes at least one integer in the list.
Result was 408040.
1
u/m42a Sep 15 '12
I got 408041. C++
1
u/ixid 0 0 Sep 15 '12
In the D language, it takes 11ms on my old computer:
The second gives a result of 408040.
1
u/Laremere 1 0 Sep 15 '12
Python solution:
Prints:
Steps:
6
Steps:
408041
1
u/[deleted] Sep 16 '12
Python, 408040
1
u/CMahaff Sep 16 '12 edited Sep 17 '12
Go - Specified 64-bit integer to make sure large numbers would work
EDIT: Works but is not efficient. Incredibly slow because I check each value.
EDIT 2: Looking at others I realized there is a much better way, faster implementation. Adding that else statement made the run time go from 51 seconds to 1.5 seconds (old computer, and it's running 64bit ints on a 32bit machine which is probably slow)
Outputs:
1
u/jtimtaylor 0 0 Sep 16 '12 edited Sep 16 '12
Python 3, prints 408040
1
u/PiereDome Sep 16 '12 edited Sep 16 '12
Javascript
jsfiddle: http://jsfiddle.net/PiereDome/CmQrw/!
Output: 408040
Time: 0.028 sec
1
u/iMalevolence Sep 17 '12
So my first solution in Java didn't finish by the time I decided to terminate it (probably 1 minute+). So I revised it and added an array that kept the current multiple through each iteration so it didn't have to reiterate through old multiples. It cut the time down to around .3 seconds or so. Given how new I am to programming, I'm glad I thought of it on my own.
returns 408041
1
u/zelf0gale Sep 19 '12 edited Sep 19 '12
In Python. Answer is 408041. I believe this is right, b/c it appears you are supposed to include the step that takes the first count that goes to, or above, the lim.
1
u/AsdfUser Sep 19 '12
C#, my answer is 408041, runs in no time:
1
u/jeremykendall Oct 01 '12
PHP, returns 408040. It's horribly slow (~130 secs) and such a memory hog I could only run it once. Any advice on making it more efficient would be quite welcome.
1
u/mrthedon Oct 01 '12
PHP (~0.5sec): 408041
1
u/Amndeep7 Oct 09 '12
Python 3
Input:
Output:
1
u/robin-gvx 0 2 Oct 19 '12
I translated Amndeep7's version from Python to Déjà Vu:
Then I converted it to a crazy stack manipulating version:
Timings from the first are around 2.2s, the second around 1.8s.
EDIT: obviously, I also get 6 and 408040, like Amndeep7.
1
u/ssuda Oct 30 '12 edited Oct 30 '12
1
u/mrestko Sep 15 '12 edited Sep 15 '12
Python, third implementation after my first two were slow as hell. My result was 408626.
EDIT: And it seems like I'm getting a horribly different answer than everyone else. Anyone see my bug?
EDIT 2: Found it. Was using "<" in the inner loop where I should have been using "<=". Answer is 408041.
0
u/[deleted] Sep 15 '12
[deleted] — view deleted comment