Sum of square and cube digits of an integer are primes: Difference between revisions
m (→{{header|Phix}}: refactored) |
m (Added Algol 68) |
||
Line 4: | Line 4: | ||
<br> |
<br> |
||
Sum of square and cube digits of an integer n are primes, where '''n<100''' |
Sum of square and cube digits of an integer n are primes, where '''n<100''' |
||
=={{header|ALGOL 68}}== |
|||
{{libheader|ALGOL 68-primes}} |
|||
<lang algol68>BEGIN # find numbers where the digit sums of the square and cube are prtime # |
|||
INT max number = 99; # maximum number to consider # |
|||
PR read "primes.incl.a68" PR |
|||
[]BOOL prime = PRIMESIEVE ( max number * max number * max number ); |
|||
# returns the sum of the digits of n # |
|||
OP DIGITSUM = ( INT n )INT: |
|||
BEGIN |
|||
INT v := ABS n; |
|||
INT result := v MOD 10; |
|||
WHILE ( v OVERAB 10 ) > 0 DO |
|||
result +:= v MOD 10 |
|||
OD; |
|||
result |
|||
END # DIGITSUM # ; |
|||
FOR i TO max number DO |
|||
INT i2 = i * i; |
|||
IF prime[ DIGITSUM i2 ] THEN |
|||
IF prime[ DIGITSUM ( i * i2 ) ] THEN |
|||
print( ( " ", whole( i, 0 ) ) ) |
|||
FI |
|||
FI |
|||
OD |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
16 17 25 28 34 37 47 52 64 |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<lang Phix>(phixonline)--> |
<!--<lang Phix>(phixonline)--> |
Revision as of 18:38, 21 December 2021
- Task
Sum of square and cube digits of an integer n are primes, where n<100
ALGOL 68
<lang algol68>BEGIN # find numbers where the digit sums of the square and cube are prtime #
INT max number = 99; # maximum number to consider # PR read "primes.incl.a68" PR []BOOL prime = PRIMESIEVE ( max number * max number * max number ); # returns the sum of the digits of n # OP DIGITSUM = ( INT n )INT: BEGIN INT v := ABS n; INT result := v MOD 10; WHILE ( v OVERAB 10 ) > 0 DO result +:= v MOD 10 OD; result END # DIGITSUM # ; FOR i TO max number DO INT i2 = i * i; IF prime[ DIGITSUM i2 ] THEN IF prime[ DIGITSUM ( i * i2 ) ] THEN print( ( " ", whole( i, 0 ) ) ) FI FI OD
END</lang>
- Output:
16 17 25 28 34 37 47 52 64
Phix
with javascript_semantics function ipsd(integer n) return is_prime(sum(sq_sub(sprintf("%d",n),'0'))) end function function scdp(integer n) return ipsd(n*n) and ipsd(n*n*n) end function pp(filter(tagset(99),scdp))
- Output:
{16,17,25,28,34,37,47,52,64}
Raku
<lang perl6>say ^100 .grep: { .².comb.sum.is-prime && .³.comb.sum.is-prime }</lang>
- Output:
(16 17 25 28 34 37 47 52 64)
Ring
<lang ring> load "stdlib.ring" see "working..." +nl
limit = 100
for n = 1 to limit
sums = 0 sumc = 0 sps = string(pow(n,2)) spc = string(pow(n,3)) for m = 1 to len(sps) sums = sums + sps[m] next for p = 1 to len(spc) sumc = sumc + spc[p] next if isprime(sums) and isprime(sumc) see "" + n + " " ok
next
see nl + "done..." + nl </lang>
- Output:
working... 16 17 25 28 34 37 47 52 64 done...
Wren
<lang ecmascript>import "./math" for Int
for (i in 1..99) {
if (Int.isPrime(Int.digitSum(i*i)) && Int.isPrime(Int.digitSum(i*i*i))) System.write("%(i) ")
} System.print()</lang>
- Output:
16 17 25 28 34 37 47 52 64
XPL0
<lang XPL0>func IsPrime(N); \Return 'true' if N is prime int N, I; [if N <= 2 then return N = 2; if (N&1) = 0 then \even >2\ return false; for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false; I:= I+1; ];
return true; ];
func SumDigits(N); \Return the sum of digits in N int N, Sum; [Sum:= 0; while N do
[N:= N/10; Sum:= Sum + rem(0); ];
return Sum; ];
int N; [for N:= 0 to 100-1 do
if IsPrime(SumDigits(N*N)) & IsPrime(SumDigits(N*N*N)) then [IntOut(0, N); ChOut(0, ^ )];
]</lang>
- Output:
16 17 25 28 34 37 47 52 64