Brilliant numbers: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) (Added Arturo implementation) |
(→{{header|Arturo}}: Added Algol 68) |
||
Line 29: | Line 29: | ||
=={{header|ALGOL 68}}== |
|||
{{libheader|ALGOL 68-primes}} |
|||
<lang algol68>BEGIN # Find Brilliant numbers - semi-primes whose two prime factors have # |
|||
# the same number of digits # |
|||
PR read "primes.incl.a68" PR # include prime utilities # |
|||
INT max prime = 1 010; # maximum prime we will consider # |
|||
# should be enough to find the first brilliant number > 10^6 # |
|||
[]BOOL prime = PRIMESIEVE max prime; # sieve of primes to max prime # |
|||
# construct a table of brilliant numbers # |
|||
[ 1 : max prime * max prime ]BOOL brilliant; |
|||
FOR n FROM LWB brilliant TO UPB brilliant DO brilliant[ n ] := FALSE OD; |
|||
# brilliant numbers where one of the fators is 2 # |
|||
brilliant[ 4 ] := TRUE; |
|||
FOR p FROM 3 BY 2 TO 7 DO brilliant[ 2 * p ] := TRUE OD; |
|||
# brilliant numbers where both factors are odd # |
|||
INT p start := 1, p end := 9; |
|||
WHILE pstart < max prime DO |
|||
FOR p FROM p start BY 2 TO p end DO |
|||
IF prime[ p ] THEN |
|||
brilliant[ p * p ] := TRUE; |
|||
FOR q FROM p + 2 BY 2 TO p end DO |
|||
IF prime[ q ] THEN |
|||
brilliant[ p * q ] := TRUE |
|||
FI |
|||
OD |
|||
FI |
|||
OD; |
|||
p start := p end + 2; |
|||
p end := ( ( p start - 1 ) * 10 ) - 1; |
|||
IF p end > max prime THEN p end := max prime FI |
|||
OD; |
|||
# show the first 100 brilliant numbers # |
|||
INT b count := 0; |
|||
FOR n TO UPB brilliant WHILE b count < 100 DO |
|||
IF brilliant[ n ] THEN |
|||
print( ( whole( n, -6 ) ) ); |
|||
IF ( b count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI |
|||
FI |
|||
OD; |
|||
# first brilliant number >= 10^n, n = 1, 2, ..., 6 # |
|||
b count := 0; |
|||
INT power of 10 := 10; |
|||
FOR n TO UPB brilliant DO |
|||
IF brilliant[ n ] THEN |
|||
b count +:= 1; |
|||
IF n >= power of 10 THEN |
|||
print( ( "First brilliant number >= ", whole( power of 10, -8 ) |
|||
, ": " , whole( n, -8 ) |
|||
, " at position " , whole( b count, -6 ) |
|||
, newline |
|||
) |
|||
); |
|||
power of 10 *:= 10 |
|||
FI |
|||
FI |
|||
OD |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
4 6 9 10 14 15 21 25 35 49 |
|||
121 143 169 187 209 221 247 253 289 299 |
|||
319 323 341 361 377 391 403 407 437 451 |
|||
473 481 493 517 527 529 533 551 559 583 |
|||
589 611 629 649 667 671 689 697 703 713 |
|||
731 737 767 779 781 793 799 803 817 841 |
|||
851 869 871 893 899 901 913 923 943 949 |
|||
961 979 989 1003 1007 1027 1037 1067 1073 1079 |
|||
1081 1121 1139 1147 1157 1159 1189 1207 1219 1241 |
|||
1247 1261 1271 1273 1333 1343 1349 1357 1363 1369 |
|||
First brilliant number >= 10: 10 at position 4 |
|||
First brilliant number >= 100: 121 at position 11 |
|||
First brilliant number >= 1000: 1003 at position 74 |
|||
First brilliant number >= 10000: 10201 at position 242 |
|||
First brilliant number >= 100000: 100013 at position 2505 |
|||
First brilliant number >= 1000000: 1018081 at position 10538 |
|||
</pre> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |