Prime triangle: Difference between revisions
Content added Content deleted
(→{{header|Go}}: Updated in line with Phix example of which this is a translation.) |
(→{{header|ALGOL 68}}: Slight simplification) |
||
Line 20: | Line 20: | ||
FI |
FI |
||
OD; |
OD; |
||
[ 1 : max number, 1 : max number ]BOOL prime pair; |
|||
FOR a TO max number DO |
|||
⚫ | |||
FOR b FROM 2 TO max number DO |
|||
⚫ | |||
⚫ | |||
prime pair[ a, a ] := FALSE |
|||
OD; |
|||
# finds the next number that can follow i or 0 if there isn't one # |
|||
PROC find next = ( INT i, INT n, INT current, []BOOL used )INT: |
|||
BEGIN |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
# returns the number of possible arrangements of the integers for a row in the prime triangle # |
# returns the number of possible arrangements of the integers for a row in the prime triangle # |
||
PROC count arrangements = ( INT n |
PROC count arrangements = ( INT n )INT: |
||
IF n < 2 THEN # no solutions for n < 2 # 0 |
IF n < 2 THEN # no solutions for n < 2 # 0 |
||
ELIF n < 4 THEN |
ELIF n < 4 THEN |
||
# for 2 and 3. there is only 1 solution: 1, 2 and 1, 2, 3 # |
# for 2 and 3. there is only 1 solution: 1, 2 and 1, 2, 3 # |
||
FOR i TO n DO print( ( whole( i, -3 ) ) ) OD; print( ( newline ) ); |
|||
⚫ | |||
⚫ | |||
1 |
1 |
||
ELSE |
ELSE |
||
# 4 or more - must find the solutions # |
# 4 or more - must find the solutions # |
||
⚫ | |||
[ 0 : n ]BOOL used; |
[ 0 : n ]BOOL used; |
||
[ 0 : n ]INT number; |
[ 0 : n ]INT number; |
||
⚫ | |||
⚫ | |||
FOR i FROM 0 TO n DO |
FOR i FROM 0 TO n DO |
||
used[ |
used[ i ] := FALSE; |
||
number[ |
number[ i ] := i MOD 2 |
||
OD; |
OD; |
||
⚫ | |||
⚫ | |||
number[ |
number[ n ] := n; |
||
used[ n ] := TRUE; |
|||
# find the intervening numbers and count the solutions # |
# find the intervening numbers and count the solutions # |
||
INT count := 0; |
INT count := 0; |
||
INT p := 2; |
INT p := 2; |
||
WHILE p |
WHILE p > 0 DO |
||
INT |
INT p1 = number[ p - 1 ]; |
||
INT |
INT current = number[ p ]; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
IF p = n - 1 THEN |
IF p = n - 1 THEN |
||
# we are at the final number before n # |
# we are at the final number before n # |
||
# it must be the final even/odd number preceded by the final odd/even number # |
|||
IF next /= 0 THEN |
|||
# possible solution # |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
# here will be a further backtrack as next is 0 ( there could only be one possible number at p - 1 ) # |
|||
FI; |
FI; |
||
IF next /= 0 THEN |
IF next /= 0 THEN |
||
# have a/another number that can appear at p # |
# have a/another number that can appear at p # |
||
used[ |
used[ current ] := FALSE; |
||
used[ |
used[ next ] := TRUE; |
||
number[ |
number[ p ] := next; |
||
p +:= 1 |
|||
# haven't found all the intervening digits yet # |
|||
p +:= 1; |
|||
number[ p ] := 0 |
|||
ELSE |
|||
⚫ | |||
⚫ | |||
⚫ | |||
FOR i TO n DO |
|||
⚫ | |||
OD; |
|||
print( ( newline ) ) |
|||
FI; |
|||
⚫ | |||
used[ number[ p ] ] := FALSE; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
ELIF p <= 2 THEN |
ELIF p <= 2 THEN |
||
# no more solutions # |
# no more solutions # |
||
p := |
p := 0 |
||
ELSE |
ELSE |
||
# can't find a number for this position, backtrack # |
# can't find a number for this position, backtrack # |
||
used[ number[ p ] ] := FALSE; |
used[ number[ p ] ] := FALSE; |
||
number[ p ] := |
number[ p ] := p MOD 2; |
||
p -:= 1 |
p -:= 1 |
||
FI |
FI |
||
Line 110: | Line 94: | ||
[ 2 : max number ]INT arrangements; |
[ 2 : max number ]INT arrangements; |
||
FOR n FROM LWB arrangements TO UPB arrangements DO |
FOR n FROM LWB arrangements TO UPB arrangements DO |
||
arrangements[ n ] := count arrangements( n |
arrangements[ n ] := count arrangements( n ) |
||
OD; |
OD; |
||
FOR n FROM LWB arrangements TO UPB arrangements DO |
FOR n FROM LWB arrangements TO UPB arrangements DO |
||
Line 116: | Line 100: | ||
OD; |
OD; |
||
print( ( newline ) ) |
print( ( newline ) ) |
||
END |
END</lang> |
||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |