Rhonda numbers: Difference between revisions
Content added Content deleted
(Added Algol 68) |
|||
Line 47: | Line 47: | ||
=={{header|ALGOL 68}}== |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN # find some Rhonda numbers: numbers n in base b such that the product # |
|||
# of the digits of n is b * the sum of the prime factors of n # |
|||
# returns the sum of the prime factors of n # |
|||
PROC factor sum = ( INT n )INT: |
|||
BEGIN |
|||
INT result := 0; |
|||
INT v := ABS n; |
|||
WHILE v > 1 AND v MOD 2 = 0 DO |
|||
result +:= 2; |
|||
v OVERAB 2 |
|||
OD; |
|||
FOR f FROM 3 BY 2 WHILE v > 1 DO |
|||
WHILE v > 1 AND v MOD f = 0 DO |
|||
result +:= f; |
|||
v OVERAB f |
|||
OD |
|||
OD; |
|||
result |
|||
END # factor sum # ; |
|||
# returns the digit product of n in the specified base # |
|||
PROC digit product = ( INT n, base )INT: |
|||
IF n = 0 THEN 0 |
|||
ELSE |
|||
INT result := 1; |
|||
INT v := ABS n; |
|||
WHILE v > 0 DO |
|||
result *:= v MOD base; |
|||
v OVERAB base |
|||
OD; |
|||
result |
|||
FI # digit product # ; |
|||
# returns TRUE if n is a Rhonda number in the specified base, # |
|||
# FALSE otherwise # |
|||
PROC is rhonda = ( INT n, base )BOOL: base * factor sum( n ) = digit product( n, base ); |
|||
# returns TRUE if n is prime, FALSE otherwise # |
|||
PROC is prime = ( INT n )BOOL: |
|||
IF n < 3 THEN n = 2 |
|||
ELIF n MOD 3 = 0 THEN n = 3 |
|||
ELIF NOT ODD n THEN FALSE |
|||
ELSE |
|||
INT f := 5; |
|||
INT f2 := 25; |
|||
INT to next := 24; |
|||
BOOL is a prime := TRUE; |
|||
WHILE f2 <= n AND is a prime DO |
|||
is a prime := n MOD f /= 0; |
|||
f +:= 2; |
|||
f2 +:= to next; |
|||
to next +:= 8 |
|||
OD; |
|||
is a prime |
|||
FI # is prime # ; |
|||
# returns a string representation of n in the specified base # |
|||
PROC to base string = ( INT n, base )STRING: |
|||
IF n = 0 THEN "0" |
|||
ELSE |
|||
INT under 10 = ABS "0"; |
|||
INT over 9 = ABS "a" - 10; |
|||
STRING result := ""; |
|||
INT v := ABS n; |
|||
WHILE v > 0 DO |
|||
INT d = v MOD base; |
|||
REPR ( d + IF d < 10 THEN under 10 ELSE over 9 FI ) +=: result; |
|||
v OVERAB base |
|||
OD; |
|||
result |
|||
FI # to base string # ; |
|||
# find the first few Rhonda numbers in non-prime bases 2 .. max base # |
|||
INT max rhonda = 10; |
|||
INT max base = 16; |
|||
FOR base FROM 2 TO max base DO |
|||
IF NOT is prime( base ) THEN |
|||
print( ( "The first ", whole( max rhonda, 0 ) |
|||
, " Rhonda numbers in base ", whole( base, 0 ) |
|||
, ":", newline |
|||
) |
|||
); |
|||
INT r count := 0; |
|||
[ 1 : max rhonda ]INT rhonda; |
|||
FOR n WHILE r count < max rhonda DO |
|||
IF is rhonda( n, base ) THEN |
|||
rhonda[ r count +:= 1 ] := n |
|||
FI |
|||
OD; |
|||
print( ( " in base 10:" ) ); |
|||
FOR i TO max rhonda DO print( ( " ", whole( rhonda[ i ], 0 ) ) ) OD; |
|||
print( ( newline ) ); |
|||
IF base /= 10 THEN |
|||
print( ( " in base ", whole( base, -2 ), ":" ) ); |
|||
FOR i TO max rhonda DO print( ( " ", to base string( rhonda[ i ], base ) ) ) OD; |
|||
print( ( newline ) ) |
|||
FI |
|||
FI |
|||
OD |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 10 Rhonda numbers in base 4: |
|||
in base 10: 10206 11935 12150 16031 45030 94185 113022 114415 191149 244713 |
|||
in base 4: 2133132 2322133 2331312 3322133 22333212 112333221 123211332 123323233 232222231 323233221 |
|||
The first 10 Rhonda numbers in base 6: |
|||
in base 10: 855 1029 3813 5577 7040 7304 15104 19136 35350 36992 |
|||
in base 6: 3543 4433 25353 41453 52332 53452 153532 224332 431354 443132 |
|||
The first 10 Rhonda numbers in base 8: |
|||
in base 10: 1836 6318 6622 10530 14500 14739 17655 18550 25398 25956 |
|||
in base 8: 3454 14256 14736 24442 34244 34623 42367 44166 61466 62544 |
|||
The first 10 Rhonda numbers in base 9: |
|||
in base 10: 15540 21054 25331 44360 44660 44733 47652 50560 54944 76857 |
|||
in base 9: 23276 31783 37665 66758 67232 67323 72326 76317 83328 126376 |
|||
The first 10 Rhonda numbers in base 10: |
|||
in base 10: 1568 2835 4752 5265 5439 5664 5824 5832 8526 12985 |
|||
The first 10 Rhonda numbers in base 12: |
|||
in base 10: 560 800 3993 4425 4602 4888 7315 8296 9315 11849 |
|||
in base 12: 3a8 568 2389 2689 27b6 29b4 4297 4974 5483 6a35 |
|||
The first 10 Rhonda numbers in base 14: |
|||
in base 10: 11475 18655 20565 29631 31725 45387 58404 58667 59950 63945 |
|||
in base 14: 4279 6b27 76cd ab27 b7c1 1277d 173da 17547 17bc2 19437 |
|||
The first 10 Rhonda numbers in base 15: |
|||
in base 10: 2392 2472 11468 15873 17424 18126 19152 20079 24388 30758 |
|||
in base 15: a97 aec 35e8 4a83 5269 5586 5a1c 5e39 735d 91a8 |
|||
The first 10 Rhonda numbers in base 16: |
|||
in base 10: 1000 1134 6776 15912 19624 20043 20355 23946 26296 29070 |
|||
in base 16: 3e8 46e 1a78 3e28 4ca8 4e4b 4f83 5d8a 66b8 718e |
|||
</pre> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |