F# solution, ~460 milliseconds runtime. Edit: Previously 620ms when I had the procedures nicely separated. The current results are from going completely imperative. And generating new filters as needed.
About as imperative as possible. But if performance and a sieve is your problem, then imperative mutations are the answer. I actually made an Elm solution first for understand-ability and then translated it into imperative F#. The Elm solution took like 40 seconds to run in the browser. Sooooo much data copying.
openSystem// Max is the number of items to wait before removal// Count is items seen since last removaltypeFilter()=[<DefaultValue>]valmutablepublicMax:int[<DefaultValue>]valmutablepublicCount:int[<EntryPoint>]letmainargs=// pre-allocating capacityletresults=Array.zeroCreate<int>71918// 1 is a givenresults.[0]<-1// index to place the next resultletmutablenextResult=1letiterationCount=1_000_000// experimentally determined 6507 filters for 1,000,000// first filter (odd numbers) built into looping logicletfilterCount=6506letfilters=Array.zeroCreate<Filter>filterCount// index to place the next filterletmutablenextFilter=0lettimer=System.Diagnostics.Stopwatch()timer.Start()letmutablen=3whilen<iterationCountdoletmutablei=0letmutablesurvived=true// check each filter to see if it will discard the number// stop as soon as it is discardedwhilei<nextFilter&&surviveddoletfilter=filters.[i]letnewCount=filter.Count+1// update filter state... this is much faster than moduloiffilter.Max=newCountthenfilter.Count<-0survived<-falseelsefilter.Count<-newCountsurvived<-truei<-i+1// survived the guantletifsurvivedthenresults.[nextResult]<-nnextResult<-nextResult+1//printfn " %i" i// create a new filter, if there is roomifnextFilter<filterCountthenfilters.[nextFilter]<-Filter(Max=n,Count=nextResult)nextFilter<-nextFilter+1n<-n+2// oddstimer.Stop()printfn"Count: %i"nextResultprintfn"Elapsed Milliseconds: %i"timer.ElapsedMillisecondsprintfn"Memory Usage: %i bytes"<|GC.GetTotalMemory(false)printfn"Garbage Collections: %i gen0, %i gen1, %i gen2"<|GC.CollectionCount(0)<|GC.CollectionCount(1)<|GC.CollectionCount(2)0// exit code
Some explanation
The idea is not to copy lists, but to filter out numbers as they come in. Each round of elimination is considered a "filter". The more filters, the lower margin of error in the results. I experimentally determined (by running the code and tweaking the number) the number of filters required for accurate results up to 1 million input numbers is 6507. In other words, 6507 rounds of elimination. You can tell the required margin of error for a given size because once you reach it, increasing the filter count further will not reduce the number of results.
Fine! This runs in about 19s on my Notebook. splice() works in place. filter() generates a copy and is an additional loop. So I thought working out of place with slice() could be better. No way. I didn't even wait for it to end. Your implementation with splice and filter is faster.
This problem made me more familiar with Go. Some nice things to learn, there. I wonder why my custom struct didn't speed the thing up (it didn't slow it down either, compared to Go slices). My Python was slow, I'd like to learn some Numpy.
Kasey Speakman raised the bar very high very early. :-)
My holidays during the "Kölsche Karneval" in Cologne are over now, so I'll throw one of my implementations in, before the work begins again.
This is the ranking of my attempts, so far:
Python (Slices): 157s
Haskell with singly-linked lists: 51 s (I didn't expect that to be fast because accessing an element by index is of course in O(n))
Java with doubly-linked list with additional state keeping to avoid O(n) indexing: 28 s
Go with doubly-linked list with additional state keeping: 10 s
Go with appending to slices similar to the Haskell deleteEveryNth function: 10 s
Go with array (in-place changes): 6.5 s
Haskell with Boxed Vectors: 3 s
C with array (in-place changes): 2 s
Haskell with Unboxed Vectors: ~600 msec
importControl.Monad.StateimportControl.Monad.Loops(untilM)importqualifiedData.Vector.UnboxedasVimportData.Vector.Unboxed((!))-- this one unqualified, please!typeIndex=InttypeVec=V.Vector-- To delete every nth element from a list:-- The state is the remaining list with elements to delete-- take n elements and leave the rest as statetakeM::V.Unboxa=>Int->State(Veca)(Veca)takeM=state.V.splitAt-- input list in state empty?end::V.Unboxa=>State(Veca)Boolend=get>>=return.V.null-- take n-1 elements and drop the nth. Repeat until the end of the list.deleteEveryNth::V.Unboxa=>Int->Veca->VecadeleteEveryNthn=V.concat.evalState(takeM(n-1)<*takeM1`untilM`end)-- To iterate the deletion passes with different step widths:-- The step width can be found at index i, then do a deletion pass over xsdeleteUnlucky::(VecInt,Index)->(VecInt,Index)deleteUnlucky(xs,i)=(deleteEveryNth(xs!i)xs,i+1)outOfRange::(VecInt,Index)->BooloutOfRange(xs,i)=i>=len||xs!i>lenwherelen=V.lengthxsluckyNumbersn=fst$untiloutOfRangedeleteUnlucky(V.fromList[1,3..n],1)main=letl=luckyNumbers1000000inprint(l,V.lengthl)
The inner loop is deleteEveryNth, which is done by monadic parsing. The combinator <* in the parsing function takeM (n-1) <* takeM 1 means: First take n-1 elements from the input, then take 1, but return only the n-1 taken at first.
The outer loop is done by until outOfRange deleteUnlucky, both functions being quite straight forward.
The code, btw, is logically exactly the same as the version with linked lists, the only difference are the "V."-functions instead of the equally named list functions, especially V.length (O(1)) instead of length (O(n)) and also important, the O(1) index operator for vectors ! instead of O(n) !! for linked lists.
PS C:\Users\Heiko\Documents\Programmieren\Haskell> .\GluecklicheZahlen-Vector.exe +RTS -s
71918
4,586,444,408 bytes allocated in the heap
31,005,008 bytes copied during GC
9,441,112 bytes maximum residency (35 sample(s))
2,704,656 bytes maximum slop
24 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 7189 colls, 0 par 0.016s 0.037s 0.0000s 0.0003s
Gen 1 35 colls, 0 par 0.000s 0.011s 0.0003s 0.0046s
INIT time 0.000s ( 0.000s elapsed)
MUT time 0.547s ( 0.536s elapsed)
GC time 0.016s ( 0.048s elapsed)
EXIT time 0.000s ( 0.002s elapsed)
Total time 0.562s ( 0.585s elapsed)
%GC time 2.8% (8.1% elapsed)
Alloc rate 8,386,641,203 bytes per MUT second
Productivity 97.2% of total user, 91.8% of total elapsed
PS C:\Users\Heiko\Documents\Programmieren\Haskell>
I'd like to get to a C implementation nearly as fast as the Haskell- and the F#-version. I don't know why my C is slower. I'll share my attempts soon...
So I pretty much went back and inlined everything in the F# solution and updated my post. Got the time way down. It should very nearly read like especially bad C code if you want to see how it does ported to C.
Your Haskell implementation is quite impressive in performance and terseness.
Great to hear that. I'm afraid I can't fully claim credit for the performance. That seems an achievement of the author of the vector library (Roman Leshchinskiy) and the authors of the Glasgow Haskell Compiler. ;-)
You sped up your program, so I let the compiler inline deleteEveryNthunsing the compiler pragma {-# INLINE deleteEveryNth #-}
I had a run in 469 ms CPU time. :-D (Of course, it all depends on hardware. But I also wanted to do the next step and profit of inlining.)
I need some more time to go through your code to understand and port it to C. Great.
I added a lot of comments. Hopefully that will help with a C translation. I just noticed you are starting with odds whereas I am filtering them out at cost of extra CPU time. I'll update my version tonight to do that also.
I'm still quite in awe of the Haskell version. It is quite expressive while performing the same. I don't understand all the specific functions or operators, but I "get" it in a general sense. Whereas I had to remove any semblance of human thought to run mine faster.
This is just a first draft, happy to have the running code in about half an hour. I bet there's a lot of room for improvements, it calculates the first million numbers in 147 seconds.
Got it down to still very slow 100 seconds by using the vec function on the sieved result to make it a vector instead of a linked list. This way the nth call inside the recur statement is way faster.
F# solution, ~460 milliseconds runtime. Edit: Previously 620ms when I had the procedures nicely separated. The current results are from going completely imperative. And generating new filters as needed.
About as imperative as possible. But if performance and a sieve is your problem, then imperative mutations are the answer. I actually made an Elm solution first for understand-ability and then translated it into imperative F#. The Elm solution took like 40 seconds to run in the browser. Sooooo much data copying.
Some explanation
The idea is not to copy lists, but to filter out numbers as they come in. Each round of elimination is considered a "filter". The more filters, the lower margin of error in the results. I experimentally determined (by running the code and tweaking the number) the number of filters required for accurate results up to 1 million input numbers is 6507. In other words, 6507 rounds of elimination. You can tell the required margin of error for a given size because once you reach it, increasing the filter count further will not reduce the number of results.
While JavaScript isn't the fastest language out there I got an implementation working as follows: Great challenge!
Finds luck numbers up to 1,000,000 in ~12s
Fine! This runs in about 19s on my Notebook. splice() works in place. filter() generates a copy and is an additional loop. So I thought working out of place with slice() could be better. No way. I didn't even wait for it to end. Your implementation with splice and filter is faster.
My goal is to see if I can make a
function*Generator but I haven't been able to figure out a consistent pattern yet that can drip out numbers..Sorry about deleting my comment, I totally broke my codeSo I changed the
.splice(j-1, 1, 0)to a direct array access and fixed thejoffset, making it 2x fasterI look forward to reading some of the solutions. A bit too beat to do it myself at the moment 😋
This problem made me more familiar with Go. Some nice things to learn, there. I wonder why my custom struct didn't speed the thing up (it didn't slow it down either, compared to Go slices). My Python was slow, I'd like to learn some Numpy.
Slow or fast..., I'm curious.
Kasey Speakman raised the bar very high very early. :-)
My holidays during the "Kölsche Karneval" in Cologne are over now, so I'll throw one of my implementations in, before the work begins again.
This is the ranking of my attempts, so far:
deleteEveryNthfunction: 10 sThe inner loop is
deleteEveryNth, which is done by monadic parsing. The combinator<*in the parsing functiontakeM (n-1) <* takeM 1means: First take n-1 elements from the input, then take 1, but return only the n-1 taken at first.The outer loop is done by
until outOfRange deleteUnlucky, both functions being quite straight forward.The code, btw, is logically exactly the same as the version with linked lists, the only difference are the "V."-functions instead of the equally named list functions, especially V.length (O(1)) instead of length (O(n)) and also important, the O(1) index operator for vectors
!instead of O(n)!!for linked lists.I'd like to get to a C implementation nearly as fast as the Haskell- and the F#-version. I don't know why my C is slower. I'll share my attempts soon...
So I pretty much went back and inlined everything in the F# solution and updated my post. Got the time way down. It should very nearly read like especially bad C code if you want to see how it does ported to C.
Your Haskell implementation is quite impressive in performance and terseness.
Great to hear that. I'm afraid I can't fully claim credit for the performance. That seems an achievement of the author of the vector library (Roman Leshchinskiy) and the authors of the Glasgow Haskell Compiler. ;-)
You sped up your program, so I let the compiler inline
deleteEveryNthunsing the compiler pragma{-# INLINE deleteEveryNth #-}I had a run in 469 ms CPU time. :-D (Of course, it all depends on hardware. But I also wanted to do the next step and profit of inlining.)
I need some more time to go through your code to understand and port it to C. Great.
I added a lot of comments. Hopefully that will help with a C translation. I just noticed you are starting with odds whereas I am filtering them out at cost of extra CPU time. I'll update my version tonight to do that also.
I'm still quite in awe of the Haskell version. It is quite expressive while performing the same. I don't understand all the specific functions or operators, but I "get" it in a general sense. Whereas I had to remove any semblance of human thought to run mine faster.
Turned out starting with odds didn't change the runtime at all really. But it led me to some shorter code that accomplished the same thing.
I also made a Go implementation that does 1,000,000 in 3s
1.6 on my Laptop. I could speed this one up to 1.2 by counting every element you set to n+1. Then:
No need for the second inner loop (counting up k), then.
Nice. My best Go attempt on an array was 6.5s. I have to think about, why this is faster.
As always: happy to provide a Clojure solution:
This is just a first draft, happy to have the running code in about half an hour. I bet there's a lot of room for improvements, it calculates the first million numbers in 147 seconds.
Got it down to still very slow 100 seconds by using the
vecfunction on the sieved result to make it a vector instead of a linked list. This way thenthcall inside therecurstatement is way faster.