Hi, I noticed this Q in StackExchange
http://codereview.stackexchange.com/questions/153288/codewars-gap-in-primes
and attempted a (naive) solution for the problem:
: next-prime ( range -- rest prime? )
dup empty?
[ f ]
[ unclip dup prime?
[ drop next-prime ] unless
] if ; recursive
:: check-gap ( gap prev rest last -- g p r l ? )
gap prev rest last
prev last - gap = ;
: (primes-gap) ( gap range last -- n1? n2? )
swap next-prime dup
[ check-gap
[ nip rot drop ]
[ [ drop ] 2dip (primes-gap) ]
if ]
[ 4 ndrop f f ] if ; recursive
: primes-gap ( gap from to -- n1? n2? )
[a,b] next-prime over
[ (primes-gap) ]
[ 3drop f f ] if ;
For that version,[ 8 3,000,000 4,000,000 primes-gap [ . ] bi@ ] timegave 59
seconds on my system, most spent on unclip.
I changed:
: range-unclip ( r -- r' 1st )
[ first 1 + ] [ last [a,b] ] [ first ] tri ;
: next-prime ( range -- rest prime? )
dup empty?
[ f ]
[ range-unclip dup prime?
[ drop next-prime ] unless
] if ; recursive
which gave only "Running time: 0.000342938 seconds"
Factor 0.98 x86.64 (1777, heads/master-ed29fbd93f, Mon Aug 8 20:45:47 2016)
[Microsoft Visual C++ 190023506] on windows 7
I know there have been some changes since then, and that I'm probably doing
things in a very sub-optimal way, but thought that there may still be something
of value here.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk