Numbers whose binary and ternary digit sums are prime: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Wren)
Line 124:
 
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
 
Line 142 ⟶ 144:
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
 
Line 167 ⟶ 171:
<pre style="height:24em">
working...
Numbers < 200 whose binary and ternary digit sums are prime:
{5,101:2,12:3}
1. {65,110101:2,2012:23}
2. {76,111110:32,2120:32}
3. {107,1010111:23,10121:23}
4. {1110,10111010:32,102101:32}
5. {1211,11001011:23,110102:23}
6. {1312,11011100:32,111110:32}
7. {1713,100011101:23,122111:53}
8. {1817,1001010001:2,200122:25}
9. {1918,1001110010:32,201200:32}
10. {2119,1010110011:3,210201:3}
11. {2521,1100110101:3,221210:53}
12. {2825,1110011001:3,1001221:25}
13. {3128,1111111100:53,10111001:32}
14. {3331,10000111111:25,10201011:3}
15. {3533,100011100001:32,10221020:53}
16. {3635,100100100011:23,11001022:25}
17. {3736,100101100100:32,11011100:32}
18. {4137,101001100101:3,11121101:53}
19. {4741,101111101001:53,12021112:5}
20. {4947,110001101111:35,12111202:5}
21. {5549,110111110001:53,20011211:35}
22. {5955,111011110111:5,20122001:53}
23. {6159,111101111011:5,20212012:5}
24. {6561,1000001111101:25,21022021:5}
25. {6765,10000111000001:32,21112102:5}
26. {6967,10001011000011:3,21202111:5}
27. {7369,10010011000101:3,22012120:5}
28. {7973,10011111001001:53,22212201:75}
29. {8279,10100101001111:35,100012221:27}
30. {8482,10101001010010:3,1001010001:2}
31. {8784,10101111010100:53,1002010010:32}
32. {9187,10110111010111:5,1010110020:3}
33. {9391,10111011011011:5,1011010101:3}
34. {9793,11000011011101:35,1012110110:53}
35. {10397,11001111100001:53,1021110121:5}
36. {107103,11010111100111:5,1022210211:75}
37. {109107,11011011101011:5,1100110222:37}
38. {115109,11100111101101:5,1102111001:53}
39. {117115,11101011110011:5,1110011021:35}
40. {121117,11110011110101:5,1111111100:53}
41. {127121,11111111111001:75,1120111111:5}
42. {129127,100000011111111:27,1121011201:5}
43. {131129,1000001110000001:32,1121211210:75}
44. {133131,1000010110000011:3,1122111212:7}
45. {137133,1000100110000101:3,1200211221:57}
46. {143137,1000111110001001:53,1202212002:75}
47. {145143,1001000110001111:35,1210112022:57}
48. {151145,1001011110010001:53,1212112101:75}
49. {155151,1001101110010111:5,1220212121:7}
50. {157155,1001110110011011:5,1221112202:7}
51. {162157,1010001010011101:35,2000012211:27}
52. {167162,1010011110100010:53,2001220000:52}
53. {171167,1010101110100111:5,2010020012:35}
54. {173171,1010110110101011:5,2010220100:53}
55. {179173,1011001110101101:5,2012220102:75}
56. {181179,1011010110110011:5,2020120122:57}
57. {185181,1011100110110101:5,2021220201:75}
58. {191185,1011111110111001:75,2100220212:57}
59. {193191,1100000110111111:37,2101121002:5}
60. {199193,1100011111000001:53,2110121011:5}
61. {199,11000111:5,21101:5}
Found 61 such numbers
done...
</pre>

Revision as of 19:42, 6 April 2021

Numbers whose binary and ternary digit sums are prime is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
Show numbers which binary and ternary digit sum are prime, where n < 200




Factor

Works with: Factor version 0.99 2021-02-05

<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

Library: Wren-math
Library: Wren-fmt
Library: Wren-seq

<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.