Numbers whose binary and ternary digit sums are prime: Difference between revisions
(Added Algol 68) |
|||
Line 5:
<br><br>
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find numbers whose digit sums in binary and ternary are prime #
# reurns a sieve of primes up to n #
PROC sieve = ( INT n )[]BOOL:
BEGIN
[ 1 : n ]BOOL p;
p[ 1 ] := FALSE; p[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO n DO p[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO n DO p[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( n ) DO
IF p[ i ] THEN FOR s FROM i * i BY i + i TO n DO p[ s ] := FALSE OD FI
OD;
p
END # prime list # ;
# returns the digit sum of n in base b #
PRIO DIGITSUM = 9;
OP DIGITSUM = ( INT n, b )INT:
BEGIN
INT d sum := 0;
INT v := ABS n;
WHILE v > 0 DO
d sum +:= v MOD b;
v OVERAB b
OD;
d sum
END # DIGITSUM # ;
INT max number = 200;
[]BOOL prime = sieve( max number );
INT n count := 0;
FOR n TO max number DO
INT d sum 2 = n DIGITSUM 2;
IF prime[ d sum 2 ] THEN
INT d sum 3 = n DIGITSUM 3;
IF prime[ d sum 3 ] THEN
# the base 2 and base 3 digit sums of n are both prime #
print( ( " ", whole( n, -3 ), IF prime[ n ] THEN "*" ELSE " " FI ) );
n count +:= 1;
IF n count MOD 14 = 0 THEN print( ( newline ) ) FI
FI
FI
OD;
print( ( newline ) );
print( ( "Found ", whole( n count, 0 ), " numbers whose binary and ternary digit sums are prime", newline ) );
print( ( " those that are themselves prime are suffixed with a ""*""", newline ) )
END</lang>
{{out}}
<pre>
5* 6 7* 10 11* 12 13* 17* 18 19* 21 25 28 31*
33 35 36 37* 41* 47* 49 55 59* 61* 65 67* 69 73*
79* 82 84 87 91 93 97* 103* 107* 109* 115 117 121 127*
129 131* 133 137* 143 145 151* 155 157* 162 167* 171 173* 179*
181* 185 191* 193* 199*
Found 61 numbers whose binary and ternary digit sums are prime
those that are themselves prime are suffixed with a "*"
</pre>
=={{header|Factor}}==
|
Revision as of 20:01, 6 April 2021
- Task
- Show numbers which binary and ternary digit sum are prime, where n < 200
ALGOL 68
<lang algol68>BEGIN # find numbers whose digit sums in binary and ternary are prime #
# reurns a sieve of primes up to n # PROC sieve = ( INT n )[]BOOL: BEGIN [ 1 : n ]BOOL p; p[ 1 ] := FALSE; p[ 2 ] := TRUE; FOR i FROM 3 BY 2 TO n DO p[ i ] := TRUE OD; FOR i FROM 4 BY 2 TO n DO p[ i ] := FALSE OD; FOR i FROM 3 BY 2 TO ENTIER sqrt( n ) DO IF p[ i ] THEN FOR s FROM i * i BY i + i TO n DO p[ s ] := FALSE OD FI OD; p END # prime list # ; # returns the digit sum of n in base b # PRIO DIGITSUM = 9; OP DIGITSUM = ( INT n, b )INT: BEGIN INT d sum := 0; INT v := ABS n; WHILE v > 0 DO d sum +:= v MOD b; v OVERAB b OD; d sum END # DIGITSUM # ; INT max number = 200; []BOOL prime = sieve( max number ); INT n count := 0; FOR n TO max number DO INT d sum 2 = n DIGITSUM 2; IF prime[ d sum 2 ] THEN INT d sum 3 = n DIGITSUM 3; IF prime[ d sum 3 ] THEN # the base 2 and base 3 digit sums of n are both prime # print( ( " ", whole( n, -3 ), IF prime[ n ] THEN "*" ELSE " " FI ) ); n count +:= 1; IF n count MOD 14 = 0 THEN print( ( newline ) ) FI FI FI OD; print( ( newline ) ); print( ( "Found ", whole( n count, 0 ), " numbers whose binary and ternary digit sums are prime", newline ) ); print( ( " those that are themselves prime are suffixed with a ""*""", newline ) )
END</lang>
- Output:
5* 6 7* 10 11* 12 13* 17* 18 19* 21 25 28 31* 33 35 36 37* 41* 47* 49 55 59* 61* 65 67* 69 73* 79* 82 84 87 91 93 97* 103* 107* 109* 115 117 121 127* 129 131* 133 137* 143 145 151* 155 157* 162 167* 171 173* 179* 181* 185 191* 193* 199* Found 61 numbers whose binary and ternary digit sums are prime those that are themselves prime are suffixed with a "*"
Factor
<lang factor>USING: combinators combinators.short-circuit formatting io lists lists.lazy math math.parser math.primes sequences ;
- dsum ( n base -- sum ) >base [ digit> ] map-sum ;
- dprime? ( n base -- ? ) dsum prime? ;
- 23prime? ( n -- ? ) { [ 2 dprime? ] [ 3 dprime? ] } 1&& ;
- l23primes ( -- list ) 1 lfrom [ 23prime? ] lfilter ;
- 23prime. ( n -- )
{ [ ] [ >bin ] [ 2 dsum ] [ 3 >base ] [ 3 dsum ] } cleave "%-8d %-9s %-6d %-7s %d\n" printf ;
"Base 10 Base 2 (sum) Base 3 (sum)" print l23primes [ 200 < ] lwhile [ 23prime. ] leach</lang>
- Output:
Base 10 Base 2 (sum) Base 3 (sum) 5 101 2 12 3 6 110 2 20 2 7 111 3 21 3 10 1010 2 101 2 11 1011 3 102 3 12 1100 2 110 2 13 1101 3 111 3 17 10001 2 122 5 18 10010 2 200 2 19 10011 3 201 3 21 10101 3 210 3 25 11001 3 221 5 28 11100 3 1001 2 31 11111 5 1011 3 33 100001 2 1020 3 35 100011 3 1022 5 36 100100 2 1100 2 37 100101 3 1101 3 41 101001 3 1112 5 47 101111 5 1202 5 49 110001 3 1211 5 55 110111 5 2001 3 59 111011 5 2012 5 61 111101 5 2021 5 65 1000001 2 2102 5 67 1000011 3 2111 5 69 1000101 3 2120 5 73 1001001 3 2201 5 79 1001111 5 2221 7 82 1010010 3 10001 2 84 1010100 3 10010 2 87 1010111 5 10020 3 91 1011011 5 10101 3 93 1011101 5 10110 3 97 1100001 3 10121 5 103 1100111 5 10211 5 107 1101011 5 10222 7 109 1101101 5 11001 3 115 1110011 5 11021 5 117 1110101 5 11100 3 121 1111001 5 11111 5 127 1111111 7 11201 5 129 10000001 2 11210 5 131 10000011 3 11212 7 133 10000101 3 11221 7 137 10001001 3 12002 5 143 10001111 5 12022 7 145 10010001 3 12101 5 151 10010111 5 12121 7 155 10011011 5 12202 7 157 10011101 5 12211 7 162 10100010 3 20000 2 167 10100111 5 20012 5 171 10101011 5 20100 3 173 10101101 5 20102 5 179 10110011 5 20122 7 181 10110101 5 20201 5 185 10111001 5 20212 7 191 10111111 7 21002 5 193 11000001 3 21011 5 199 11000111 5 21101 5
Phix
function to_base(atom n, integer base) string result = "" while true do result &= remainder(n,base) n = floor(n/base) if n=0 then exit end if end while return result end function function prime23(integer n) return is_prime(sum(to_base(n,2))) and is_prime(sum(to_base(n,3))) end function sequence res = filter(tagset(199),prime23) printf(1,"%d numbers found: %V\n",{length(res),shorten(res,"",5)})
- Output:
61 numbers found: {5,6,7,10,11,"...",181,185,191,193,199}
Ring
<lang ring> load "stdlib.ring"
see "working..." + nl see "Numbers < 200 whose binary and ternary digit sums are prime:" + nl
decList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
num = 0 limit = 200
for n = 1 to limit
strBin = decimaltobase(n,2) strTer = decimaltobase(n,3) sumBin = 0 for m = 1 to len(strBin) sumBin = sumBin + number(strBin[m]) next sumTer = 0 for m = 1 to len(strTer) sumTer = sumTer + number(strTer[m]) next if isprime(sumBin) and isprime(sumTer) num = num + 1 see "" + num + ". {" + n + "," + strBin + ":" + sumBin + "," + strTer + ":" + sumTer + "}" + nl ok
next
see "Found " + num + " such numbers" + nl see "done..." + nl
func decimaltobase(nr,base)
binList = [] binary = 0 remainder = 1 while(nr != 0) remainder = nr % base ind = find(decList,remainder) rem = baseList[ind] add(binList,rem) nr = floor(nr/base) end binlist = reverse(binList) binList = list2str(binList) binList = substr(binList,nl,"") return binList
</lang>
- Output:
working... Numbers < 200 whose binary and ternary digit sums are prime: 1. {5,101:2,12:3} 2. {6,110:2,20:2} 3. {7,111:3,21:3} 4. {10,1010:2,101:2} 5. {11,1011:3,102:3} 6. {12,1100:2,110:2} 7. {13,1101:3,111:3} 8. {17,10001:2,122:5} 9. {18,10010:2,200:2} 10. {19,10011:3,201:3} 11. {21,10101:3,210:3} 12. {25,11001:3,221:5} 13. {28,11100:3,1001:2} 14. {31,11111:5,1011:3} 15. {33,100001:2,1020:3} 16. {35,100011:3,1022:5} 17. {36,100100:2,1100:2} 18. {37,100101:3,1101:3} 19. {41,101001:3,1112:5} 20. {47,101111:5,1202:5} 21. {49,110001:3,1211:5} 22. {55,110111:5,2001:3} 23. {59,111011:5,2012:5} 24. {61,111101:5,2021:5} 25. {65,1000001:2,2102:5} 26. {67,1000011:3,2111:5} 27. {69,1000101:3,2120:5} 28. {73,1001001:3,2201:5} 29. {79,1001111:5,2221:7} 30. {82,1010010:3,10001:2} 31. {84,1010100:3,10010:2} 32. {87,1010111:5,10020:3} 33. {91,1011011:5,10101:3} 34. {93,1011101:5,10110:3} 35. {97,1100001:3,10121:5} 36. {103,1100111:5,10211:5} 37. {107,1101011:5,10222:7} 38. {109,1101101:5,11001:3} 39. {115,1110011:5,11021:5} 40. {117,1110101:5,11100:3} 41. {121,1111001:5,11111:5} 42. {127,1111111:7,11201:5} 43. {129,10000001:2,11210:5} 44. {131,10000011:3,11212:7} 45. {133,10000101:3,11221:7} 46. {137,10001001:3,12002:5} 47. {143,10001111:5,12022:7} 48. {145,10010001:3,12101:5} 49. {151,10010111:5,12121:7} 50. {155,10011011:5,12202:7} 51. {157,10011101:5,12211:7} 52. {162,10100010:3,20000:2} 53. {167,10100111:5,20012:5} 54. {171,10101011:5,20100:3} 55. {173,10101101:5,20102:5} 56. {179,10110011:5,20122:7} 57. {181,10110101:5,20201:5} 58. {185,10111001:5,20212:7} 59. {191,10111111:7,21002:5} 60. {193,11000001:3,21011:5} 61. {199,11000111:5,21101:5} Found 61 such numbers done...
Wren
<lang ecmascript>import "/math" for Int import "/fmt" for Fmt import "/seq" for Lst
var numbers = [] for (i in 2..199) {
var bds = Int.digitSum(i, 2) if (Int.isPrime(bds)) { var tds = Int.digitSum(i, 3) if (Int.isPrime(tds)) numbers.add(i) }
} System.print("Numbers < 200 whose binary and ternary digit sums are prime:") for (chunk in Lst.chunks(numbers, 14)) Fmt.print("$4d", chunk) System.print("\nFound %(numbers.count) such numbers.")</lang>
- Output:
Numbers < 200 whose binary and ternary digit sums are prime: 5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199 Found 61 such numbers.