Hi Samuel,

This unit test from math.primes fails sometimes:

[ 5 t { 14 14 14 14 14 } ]
[ 5 15 unique-primes [ length ] [ [ prime? ] all? ] [ [ log2 ] map ]
tri ] unit-test

I assume the bug is in random-prime, which is called by unique-primes.

If you call '15 random-prime', it generates an integer no greater than
32767, and then calls next-prime on it. However,

( scratchpad ) 32749 32771 [a,b] [ prime? ] filter .
V{ 32749 32771 }

So if random-bits returns a number greater than 32749, then next-prime
will return 32771 (> 32767) which has 16 bits, not 15 as required. A
similar situation can occur with any number of bits.

I believe the correct fix would be for random-prime to call itself
again if the result of next-prime is too big, but I'm not sure. How
important is it that the result has <= n bits? Also would my
suggestion above skew the distribution somehow?

Slava

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to