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

→‎{{header|Python}}: Added a solution in Python
(Add J)
(→‎{{header|Python}}: Added a solution in Python)
Line 307:
61 numbers found: {5,6,7,10,11,"...",181,185,191,193,199}
</pre>
 
=={{header|Python}}==
<lang python>'''Binary and Ternary digit sums both prime'''
 
 
# digitSumsPrime :: [Int] -> Int -> Bool
def digitSumsPrime(bases):
'''True if the digits of n in each
given base have prime sums.
'''
def test(x):
def p(base):
return isPrime(digitSum(base)(x))
return p
 
def go(n):
return all(
map(test(n), bases)
)
return go
 
 
# digitSum :: Int -> Int -> Int
def digitSum(base):
'''The sum of the digits of n in a given base.
'''
def go(n):
q, r = divmod(n, base)
return go(q) + r if n else 0
return go
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Matching integers in the range [1..199]'''
xs = [
str(n) for n in range(1, 200)
if digitSumsPrime([2, 3])(n)
]
print(f'{len(xs)} matches in [1..199]\n')
print(table(10)(xs))
 
 
# ----------------------- GENERIC ------------------------
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divible, the final list will be shorter than n.
'''
def go(xs):
return (
xs[i:n + i] for i in range(0, len(xs), n)
) if 0 < n else None
return go
 
 
# isPrime :: Int -> Bool
def isPrime(n):
'''True if n is prime.'''
if n in (2, 3):
return True
if 2 > n or 0 == n % 2:
return False
if 9 > n:
return True
if 0 == n % 3:
return False
 
def p(x):
return 0 == n % x or 0 == n % (2 + x)
 
return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))
 
 
# table :: Int -> [String] -> String
def table(n):
'''A list of strings formatted as
right-justified rows of n columns.
'''
def go(xs):
w = len(xs[-1])
return '\n'.join(
' '.join(row) for row in chunksOf(n)([
s.rjust(w, ' ') for s in xs
])
)
return go
 
 
# MAIN ---
if __name__ == '__main__':
main()
</lang>
<pre>61 matches in [1..199]
 
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</pre>
 
=={{header|Raku}}==
9,655

edits