]> gitweb.factorcode.org Git - factor.git/blob - basis/math/primes/safe/safe.factor
Switch to https urls
[factor.git] / basis / math / primes / safe / safe.factor
1 ! Copyright (C) 2009 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: combinators.short-circuit kernel math math.functions
4 math.primes random ;
5 IN: math.primes.safe
6
7 <PRIVATE
8
9 : safe-prime-candidate? ( n -- ? )
10     1 + 6 divisor? ;
11
12 : next-safe-prime-candidate ( n -- candidate )
13     next-prime dup safe-prime-candidate?
14     [ next-safe-prime-candidate ] unless ;
15
16 PRIVATE>
17
18 : safe-prime? ( q -- ? )
19     { [ prime? ] [ 1 - 2 / prime? ] } 1&& ;
20
21 : next-safe-prime ( n -- q )
22     next-safe-prime-candidate
23     dup safe-prime? [ next-safe-prime ] unless ;
24
25 : random-safe-prime ( numbits -- p )
26     random-bits* next-safe-prime ;