Numbers whose binary and ternary digit sums are prime: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: simplified the code.) |
|||
Line 996: | Line 996: | ||
call genP /*build array of semaphores for primes.*/ |
call genP /*build array of semaphores for primes.*/ |
||
w= 10 /*width of a number in any column. */ |
w= 10 /*width of a number in any column. */ |
||
title= ' positive integers whose binary and ternary digit sums are prime, N < ' commas(n) |
|||
if cols>0 then say ' index │'center( |
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) ) /*maybe show title.*/ |
||
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') /*maybe show sep. */ |
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') /*maybe show sep. */ |
||
found= 0; idx= 1 /*initialize # of finds and the index. */ |
|||
$= /*a list of numbers found (so far). */ |
$= /*a list of numbers found (so far). */ |
||
do j=1 for n-1 /*find #s whose B2 & B3 sums are prime.*/ |
do j=1 for n-1 /*find #s whose B2 & B3 sums are prime.*/ |
||
b2= sumDig( tBase(j, 2) ); if \!.b2 then iterate /*convert to base2, sum digits.*/ |
b2= sumDig( tBase(j, 2) ); if \!.b2 then iterate /*convert to base2, sum digits.*/ |
||
b3= sumDig( tBase(j, 3) ); if \!.b3 then iterate /* " " base3 " " */ |
b3= sumDig( tBase(j, 3) ); if \!.b3 then iterate /* " " base3 " " */ |
||
found= found + 1 /*bump the number of found integers. */ |
|||
if cols<1 then iterate /*Only showing the summary? Then skip.*/ |
if cols<1 then iterate /*Only showing the summary? Then skip.*/ |
||
$= $ right( commas(j), w) /*add a commatized integer ───► $ list.*/ |
$= $ right( commas(j), w) /*add a commatized integer ───► $ list.*/ |
||
if |
if found//cols\==0 then iterate /*have we populated a line of output? */ |
||
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ |
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ |
||
idx= idx + cols /*bump the index count for the output*/ |
idx= idx + cols /*bump the index count for the output*/ |
||
Line 1,013: | Line 1,013: | ||
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
||
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') |
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') /*show foot sep ? */ |
||
say |
say |
||
say 'Found ' commas( |
say 'Found ' commas(found) title /*show summary. */ |
||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 1,021: | Line 1,021: | ||
sumDig: procedure; parse arg x 1 s 2;do j=2 for length(x)-1;s=s+substr(x,j,1);end;return s |
sumDig: procedure; parse arg x 1 s 2;do j=2 for length(x)-1;s=s+substr(x,j,1);end;return s |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
genP: |
genP: @= 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 |
||
!.=0; do p=1 for words(@); _= word(@, p); !._= 1; end; return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
tBase: procedure; parse arg x,toBase; |
tBase: procedure; parse arg x,toBase; y=; $= 0123456789 |
||
do while x>=toBase; y= substr($, x//toBase+1, 1)y; x= x % toBase |
do while x>=toBase; y= substr($, x//toBase+1, 1)y; x= x % toBase |
||
end /*while*/ |
end /*while*/ |