-
Notifications
You must be signed in to change notification settings - Fork 1
/
p27.forth
70 lines (56 loc) · 1.03 KB
/
p27.forth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Project Euler problem 27 in RetroForth (Unu format)
## Prime testing
~~~
(n,m--b)
:divides? /mod drop #0 eq? ;
(a,b--a,b,a,b)
:2dup over over ;
(a,b,c--)
:drop3 drop drop drop ;
(n--)
:show n:put nl ;
(n--b)
:pprime?
dup n:sqrt #2
repeat
2dup lt? [ drop3 TRUE ] if;
rot 2dup swap divides? [ drop3 FALSE ] if;
rot rot
n:inc
again ;
(n--b)
:prime? dup #0 gt? [ pprime? ] [ drop FALSE ] choose ;
~~~
## Computing consecutive primes of: n^2 + An + B
~~~
'A var
'B var
(n--m)
:compute @B swap dup @A * swap dup * + + ; (n^2+An+B)
(--n)
:consecutive-primes #0 repeat dup compute prime? 0; drop n:inc again ;
~~~
## Finding the solution
~~~
'BestA var
'BestB var
'BestCount var #0 !BestCount
'Limit var #1000 !Limit
@Limit n:negate #1 + !A
@Limit #2 * #1 - [
@Limit n:negate !B
@Limit #2 * #1 + [
consecutive-primes dup @BestCount gt? [
@A !BestA
@B !BestB
!BestCount
] [
drop
] choose
@B n:inc !B
] times
@A n:inc !A
] times
@BestA n:put sp @BestB n:put sp @BestCount n:put nl
@BestA @BestB * n:put nl
~~~