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

From Rosetta Code
Content added Content deleted
(Created page with "{{Draft task}} Category:Prime Numbers ;Task:Show numbers which binary and ternary digit sum are prime, where '''n < 200''' <br><br> =={{header|Ring}}== <lang ring> load "...")
 
(Add Factor)
Line 4: Line 4:
;Task:Show numbers which binary and ternary digit sum are prime, where '''n < 200'''
;Task:Show numbers which binary and ternary digit sum are prime, where '''n < 200'''
<br><br>
<br><br>


=={{header|Factor}}==
{{works with|Factor|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>
{{out}}
<pre style="height:24em">
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
</pre>

=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<lang ring>

Revision as of 13:57, 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

Ring

<lang ring> load "stdlib.ring"

see "working..." + 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"]

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)
      see "{" + n + "," + strBin + ":" + sumBin + "," + strTer + ":" + sumTer + "}" + nl
   ok

next

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...
{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}
done...