Sum of square and cube digits of an integer are primes: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Add C)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(29 intermediate revisions by 18 users not shown)
Line 13:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight 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
Line 41:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
16 17 25 28 34 37 47 52 64
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin
integer procedure digitSum( integer value n ) ;
begin
integer sum, v, vOver10;
sum := 0;
v := n;
while v > 0 do begin
vover10 := v div 10;
sum := sum + ( v - ( vover10 * 10 ) );
v := vover10
end while_v_gt_0 ;
sum
end digitSum ;
logical procedure isPrime( integer value n ) ;
if n < 2 then false
else if not odd( n ) then n = 2
else begin
logical prime;
integer p;
prime := true;
p := 3;
while p * p <= n and prime do begin
prime := n rem p not = 0;
p := p + 2;
end while_p2_le_n_and_prime ;
prime
end isPrime ;
for i := 1 until 99 do begin
integer i2;
i2 := i * i;
if isPrime( digitSum( i2 ) ) then begin;
if isPrime( digitSum( i2 * i ) ) then writeon( i_w := 1, s_w := 1, i )
end
end
end.
</syntaxhighlight>
{{out}}
<pre>
16 17 25 28 34 37 47 52 64
</pre>
 
=={{header|APL}}==
<langsyntaxhighlight lang="apl">(⊢(/⍨)∧/∘((2=0+.=⍳|⊢)∘(+/⍎¨∘⍕)¨*∘2 3)¨)⍳100</langsyntaxhighlight>
{{out}}
<pre>16 17 25 28 34 37 47 52 64</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">print select 1..100 'x ->
and? [prime? sum digits x^2]
[prime? sum digits x^3]</syntaxhighlight>
 
{{out}}
 
<pre>16 17 25 28 34 37 47 52 64</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUM_OF_SQUARE_AND_CUBE_DIGITS_OF_AN_INTEGER_ARE_PRIMES.AWK
# converted from FreeBASIC
BEGIN {
start = 1
stop = 99
for (i=start; i<=stop; i++) {
if (is_prime(digit_sum(i^3,10)) && is_prime(digit_sum(i^2,10))) {
printf("%5d%1s",i,++count%10?"":"\n")
}
}
printf("\nSum of square and cube digits are prime %d-%d: %d\n",start,stop,count)
exit(0)
}
function digit_sum(n,b, s) { # digital sum of n in base b
while (n) {
s += n % b
n = int(n/b)
}
return(s)
}
function is_prime(n, d) {
d = 5
if (n < 2) { return(0) }
if (n % 2 == 0) { return(n == 2) }
if (n % 3 == 0) { return(n == 3) }
while (d*d <= n) {
if (n % d == 0) { return(0) }
d += 2
if (n % d == 0) { return(0) }
d += 4
}
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
16 17 25 28 34 37 47 52 64
Sum of square and cube digits are prime 1-99: 9
</pre>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">
function digsum(byval n as uinteger, b as const uinteger) as uinteger
'digital sum of n in base b
dim as integer s
while n
s+=n mod b
n\=b
wend
return s
end function
 
function isprime(n as const uinteger) as boolean
if n<2 then return false
if n<4 then return true
if n mod 2 = 0 then return false
dim as uinteger i = 3
while i*i<=n
if n mod i = 0 then return false
i+=2
wend
return true
end function
 
for n as uinteger = 1 to 99
if isprime(digsum(n^3,10)) andalso isprime(digsum(n^2,10)) then print n;" ";
next n</syntaxhighlight>
{{out}}<pre>16 17 25 28 34 37 47 52 64</pre>
 
==={{header|QuickBASIC}}===
{{trans|XPL0}}
<syntaxhighlight lang="qbasic">
' Sum of square and cube digits of an integer are primes
DECLARE FUNCTION SumDigits% (Num&)
DECLARE FUNCTION IsPrime% (Num%)
CONST TRUE% = -1, FALSE% = 0
FOR N = 0 TO 99
IF IsPrime%(SumDigits%(N * N)) AND IsPrime%(SumDigits%(N * N * N)) THEN PRINT N;
NEXT N
PRINT
END
 
FUNCTION IsPrime% (Num%)
IF Num% < 2 THEN
IsPrime% = FALSE%
ELSEIF Num% = 2 THEN
IsPrime% = TRUE%
ELSEIF Num% MOD 2 = 0 THEN
IsPrime% = FALSE%
ELSE
I% = 3: FoundFac% = FALSE%
WHILE I% * I% <= Num% AND NOT FoundFac%
IF Num% MOD I% = 0 THEN FoundFac% = TRUE%
I% = I% + 2
WEND
IsPrime% = NOT FoundFac%
END IF
END FUNCTION
 
FUNCTION SumDigits% (Num&)
Sum% = 0
WHILE Num& <> 0
Sum% = Sum% + Num& MOD 10
Num& = Num& \ 10
WEND
SumDigits% = Sum%
END FUNCTION
</syntaxhighlight>
{{out}}
<pre>
16 17 25 28 34 37 47 52 64
</pre>
 
==={{header|Tiny BASIC}}===
{{works with|TinyBasic}}
This can only go up to 31 because 32^3 is too big to fit in a signed 16-bit int.
<syntaxhighlight lang="basic">10 REM Sum of square and cube digits of an integer are primes
20 REM N, the number to be tested
30 REM D, the digital sum of its square or cube
40 REM T, temporary variable
50 REM Z, did D test as prime or not
60 LET N = 1
70 LET T = N * N * N
80 GOSUB 200
90 GOSUB 260
100 IF Z = 0 THEN GOTO 160
110 LET T = N * N
120 GOSUB 200
130 GOSUB 260
140 IF Z = 0 THEN GOTO 160
150 PRINT N
160 IF N = 31 THEN END
170 LET N = N + 1
180 GOTO 70
190 REM Calculate sum of digits
200 LET D = 0
210 IF T = 0 THEN RETURN
220 LET D = D + (T - (T / 10) * 10)
230 LET T = T / 10
240 GOTO 210
250 REM Check if is prime
260 LET Z = 0
270 IF D < 2 THEN RETURN
280 LET Z = 1
290 IF D < 4 THEN RETURN
300 LET Z = 0
310 IF (D / 2) * 2 = D THEN RETURN
320 LET T = 1
330 LET T = T + 2
340 IF T * T > D THEN GOTO 370
350 IF (D / T) * T = D THEN RETURN
360 GOTO 330
370 LET Z = 1
380 RETURN</syntaxhighlight>
{{out}}<pre>16
17
25
28</pre>
 
==={{header|Yabasic}}===
{{trans|Ring}}
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Sum_of_square_and_cube_digits_of_an_integer_are_primes
// by Galileo, 04/2022
 
sub isPrime(n)
local i
if n < 4 return n >= 2
for i = 2 to sqrt(n)
if not mod(n, i) return false
next
return true
end sub
limit = 100
for n = 1 to limit
sums = 0
sumc = 0
sps$ = str$(n^2)
spc$ = str$(n^3)
for m = 1 to len(sps$)
sums = sums + val(mid$(sps$, m, 1))
next
for p = 1 to len(spc$)
sumc = sumc + val(mid$(spc$, p, 1))
next
if isPrime(sums) and isPrime(sumc) then
print n, " ";
endif
next
print</syntaxhighlight>
{{out}}
<pre>16 17 25 28 34 37 47 52 64
---Program done, press RETURN---</pre>
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn"># 'Library' functions from BQNCrate
Digits ← 10 {⌽𝕗|⌊∘÷⟜𝕗⍟(↕1+·⌊𝕗⋆⁼1⌈⊢)}
Prime ← 2=·+´0=(1+↕)⊸|
 
(∧˝∘⍉∘((Prime +´∘Digits)¨⋆⌜⟜2‿3))⊸/↕100</langsyntaxhighlight>
{{out}}
<pre>⟨ 16 17 25 28 34 37 47 52 64 ⟩</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
 
Line 85 ⟶ 337:
printf("\n");
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>16 17 25 28 34 37 47 52 64</pre>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">digit_sum = proc (n: int) returns (int)
sum: int := 0
while n>0 do
Line 122 ⟶ 374:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>16 17 25 28 34 37 47 52 64</pre>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. SQUARE-CUBE-DIGITS-PRIME.
Line 190 ⟶ 442:
CHECK-DIVISOR.
DIVIDE SUM BY DIVISOR GIVING DIV-TEST.
IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.</langsyntaxhighlight>
{{out}}
<pre>16
Line 201 ⟶ 453:
52
64</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
procedure GetDigits(N: integer; var IA: TIntegerDynArray);
{Get an array of the integers in a number}
{Numbers returned from least to most significant}
var T,I,DC: integer;
begin
DC:=Trunc(Log10(N))+1;
SetLength(IA,DC);
for I:=0 to DC-1 do
begin
T:=N mod 10;
N:=N div 10;
IA[I]:=T;
end;
end;
 
 
procedure SquareCubeDigitsPrime(Memo: TMemo);
var Dg1,Dg2: TIntegerDynArray;
var SQ,CU: integer;
var Sum1,Sum2: integer;
var I,J: integer;
var S: string;
begin
S:='';
for I:=1 to 100-1 do
begin
SQ:=I*I;
CU:=I*I*I;
GetDigits(SQ,Dg1);
GetDigits(CU,Dg2);
Sum1:=0;
for J:=0 to High(Dg1) do Sum1:=Sum1+Dg1[J];
Sum2:=0;
for J:=0 to High(Dg2) do Sum2:=Sum2+Dg2[J];
if IsPrime(Sum1) and IsPrime(Sum2) then
S:=S+' '+IntToStr(I);
end;
Memo.Lines.Add(S);
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
16 17 25 28 34 37 47 52 64
 
Elapsed Time: 1.809 ms.
</pre>
 
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Sum of square and cube digits of an integer are primes. Nigel Galloway: December 22nd., 2021
let rec fN g=function 0->g |n->fN(g+n%10)(n/10)
[1..99]|>List.filter(fun g->isPrime(fN 0 (g*g)) && isPrime(fN 0 (g*g*g)))|>List.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
16 17 25 28 34 37 47 52 64
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<syntaxhighlight lang="factor">USING: kernel math math.functions math.primes math.text.utils prettyprint sequences ;
 
100 <iota> [ [ sq ] [ 3 ^ ] bi [ 1 digit-groups sum prime? ] both? ] filter .</syntaxhighlight>
{{out}}
<pre>
V{ 16 17 25 28 34 37 47 52 64 }
</pre>
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 F I=1,100;D 2
01.20 Q
 
Line 236 ⟶ 555:
05.10 S Z=S/D
05.20 I (FITR(Z)-Z)5.3;S C=-1
05.30 R</langsyntaxhighlight>
{{out}}
<pre>= 16
Line 248 ⟶ 567:
= 64</pre>
 
=={{header|FreeBASICGo}}==
{{libheader|Go-rcu}}
<lang freebasic>
<syntaxhighlight lang="go">package main
function digsum(byval n as uinteger, b as const uinteger) as uinteger
'digital sum of n in base b
dim as integer s
while n
s+=n mod b
n\=b
wend
return s
end function
 
import (
function isprime(n as const uinteger) as boolean
"fmt"
if n<2 then return false
"rcu"
if n<4 then return true
)
if n mod 2 = 0 then return false
dim as uinteger i = 3
while i*i<=n
if n mod i = 0 then return false
i+=2
wend
return true
end function
 
func main() {
for n as uinteger = 1 to 99
for i := 1; i < 100; i++ {
if isprime(digsum(n^3,10)) andalso isprime(digsum(n^2,10)) then print n;" ";
if !rcu.IsPrime(rcu.DigitSum(i*i, 10)) {
next n</lang>
continue
{{out}}<pre>16 17 25 28 34 37 47 52 64</pre>
}
if rcu.IsPrime(rcu.DigitSum(i*i*i, 10)) {
fmt.Printf("%d ", i)
}
}
fmt.Println()
}</syntaxhighlight>
 
{{out}}
<pre>
16 17 25 28 34 37 47 52 64
</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.Numbers.Primes (isPrime)
 
---- SQUARE AND CUBE BOTH HAVE PRIME DECIMAL DIGIT SUMS --
 
p :: Int -> Bool
Line 285 ⟶ 603:
((&&) . primeDigitSum . (^ 2))
<*> (primeDigitSum . (^ 3))
 
 
--------------------------- TEST -------------------------
main :: IO ()
main = print $ filter p [2 .. 99]
 
 
------------------------- GENERIC ------------------------
Line 300 ⟶ 616:
where
go 0 = 0
go n = uncurry (+) (. first go $ quotRem n base)</langsyntaxhighlight>
{{Out}}
<pre>[16,17,25,28,34,37,47,52,64]</pre>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">((1*./@p:[:+/@|:10#.^:_1^&2 3)"0#]) i.100</langsyntaxhighlight>
{{out}}
<pre>16 17 25 28 34 37 47 52 64</pre>
 
=={{header|jq}}==
{{works with|jq}}
''Also works with gojq and fq''
 
'''Preliminaries'''
<syntaxhighlight lang=jq>
def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else 23
| until( (. * .) > $n or ($n % . == 0); .+2)
| . * . > $n
end;
 
# emit an array of the decimal digits of the integer input, least significant digit first.
def digits:
recurse( if . >= 10 then ((. - (.%10)) / 10) else empty end) | . % 10;
 
def digitSum:
def add(s): reduce s as $_ (0; .+$_);
add(digits);
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang=jq>
range(1;100)
| (.*.) as $sq
| select( ($sq | digitSum | is_prime) and ($sq * . | digitSum | is_prime ) )
</syntaxhighlight>
{{output}}
<pre>
16
17
25
28
34
37
47
52
64
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
is_sqcubsumprime(n) = isprime(sum(digits(n*n))) && isprime(sum(digits(n*n*n)))
println(filter(is_sqcubsumprime, 1:100)) # [16, 17, 25, 28, 34, 37, 47, 52, 64]
</syntaxhighlight>
</lang>
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
BOOLEAN PRIME
Line 349 ⟶ 714:
VECTOR VALUES FMT = $I2*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre>16
Line 360 ⟶ 725:
52
64</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">const Primes = {2, 3, 5, 7, 11, 13, 17, 19}
 
func digitSum(n: Positive): int =
## Return the sum of digits of "n".
var n = n.Natural
while n != 0:
result += n mod 10
n = n div 10
 
for n in 5..99:
let n² = n * n
if digitSum(n²) in Primes and digitSum(n * n²) in Primes:
stdout.write n, ' '
echo()
</syntaxhighlight>
 
{{out}}
<pre>16 17 25 28 34 37 47 52 64 </pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let is_prime n =
let rec test x =
let q = n / x in x > q || x * q <> n && n mod (x + 2) <> 0 && test (x + 6)
in if n < 5 then n lor 1 = 3 else n land 1 <> 0 && n mod 3 <> 0 && test 5
 
let rec digit_sum n =
if n < 10 then n else n mod 10 + digit_sum (n / 10)
 
let is_square_and_cube_digit_sum_prime n =
is_prime (digit_sum (n * n)) && is_prime (digit_sum (n * n * n))
 
let () =
Seq.ints 1 |> Seq.take_while ((>) 100)
|> Seq.filter is_square_and_cube_digit_sum_prime
|> Seq.iter (Printf.printf " %u") |> print_newline</syntaxhighlight>
{{out}}
<pre> 16 17 25 28 34 37 47 52 64</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Sum_of_square_and_cube_digits_of_an_integer_are_primes
Line 372 ⟶ 776:
is_prime( vecsum( split //, $_ ** 2 ) ) &&
is_prime( vecsum( split //, $_ ** 3 ) ), 1 .. 100;
print "@results\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 379 ⟶ 783:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">ipsd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">scdp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">ipsd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ipsd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">99</span><span style="color: #0000FF;">),</span><span style="color: #000000;">scdp</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
{16,17,25,28,34,37,47,52,64}
</pre>
 
 
=={{header|PL/0}}==
<syntaxhighlight lang="pascal">
const maxnumber = 99;
var n, sum, prime, i, i2, i3;
procedure sumdigitsofn;
var v, vover10;
begin
sum := 0;
v := n;
while v > 0 do begin
vover10 := v / 10;
sum := sum + ( v - ( vover10 * 10 ) );
v := vover10
end
end;
procedure isnprime;
var p;
begin
prime := 1;
if n < 2 then prime := 0;
if n > 2 then begin
prime := 0;
if odd( n ) then prime := 1;
p := 3;
while p * p <= n * prime do begin
if n - ( ( n / p ) * p ) = 0 then prime := 0;
p := p + 2;
end
end
end;
begin
i := 0;
while i <= maxnumber do begin
i := i + 1;
i2 := i * i;
n := i2;
call sumdigitsofn;
n := sum;
call isnprime;
if prime = 1 then begin
n := i2 * i;
call sumdigitsofn;
n := sum;
call isnprime;
if prime = 1 then ! i
end
end
end.
</syntaxhighlight>
{{out}}
<pre>
16
17
25
28
34
37
47
52
64
</pre>
 
=={{header|Python}}==
===Procedural===
<syntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
 
def digSum(n, b):
s = 0
while n:
s += (n % b)
n = n // b
return s
 
if __name__ == '__main__':
for n in range(11, 99):
if isPrime(digSum(n**3, 10)) and isPrime(digSum(n**2, 10)):
print(n, end = " ")
</syntaxhighlight>
{{out}}
<pre>16 17 25 28 34 37 47 52 64</pre>
 
===Functional===
<syntaxhighlight lang="python">'''Square and cube both have prime decimal digit sums'''
 
# p :: Int -> Bool
def p(n):
'''True if the square and the cube of N both have
decimal digit sums which are prime.
'''
return primeDigitSum(n ** 2) and primeDigitSum(n ** 3)
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Matches in the range [1..99]'''
print([
x for x in range(2, 100)
if p(x)
])
 
 
# ----------------------- GENERIC ------------------------
 
# primeDigitSum :: Int -> Bool
def primeDigitSum(n):
'''True if the sum of the decimal digits of n is prime.
'''
return isPrime(digitSum(10)(n))
 
 
# digitSum :: 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
 
 
# 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 q(x):
return 0 == n % x or 0 == n % (2 + x)
 
return not any(map(q, range(5, 1 + int(n ** 0.5), 6)))
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre>[16, 17, 25, 28, 34, 37, 47, 52, 64]</pre>
 
=={{header|Quackery}}==
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="quackery"> [ 0 swap
[ dup while
10 /mod
rot + swap
again ]
drop ] is digitsum ( n --> n )
 
98 times
[ i^ 1+ 2 **
digitsum isprime if
[ i^ 1+ 3 **
digitsum isprime if
[ i^ 1+ echo sp ] ] ]</syntaxhighlight>
 
{{out}}
 
<pre>16 17 25 28 34 37 47 52 64</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>say ^100 .grep: { .².comb.sum.is-prime && .³.comb.sum.is-prime }</langsyntaxhighlight>
{{out}}
<pre>(16 17 25 28 34 37 47 52 64)</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." +nl
Line 419 ⟶ 997:
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 427 ⟶ 1,005:
</pre>
 
=={{header|TinyBASICRuby}}==
<syntaxhighlight lang="ruby">require 'prime'
This can only go up to 31 because 32^3 is too big to fit in a signed 16-bit int.
<lang tinybasic>REM N, the number to be tested
REM D, the digital sum of its square or cube
REM T, temporary variable
REM Z, did D test as prime or not
 
p (1..100).select{|n|(n*n).digits.sum.prime? && (n**3).digits.sum.prime?}</syntaxhighlight>
LET N = 1
{{out}}
10 LET T = N*N*N
<pre>
GOSUB 20
[16, 17, 25, 28, 34, 37, 47, 52, 64]
GOSUB 30
</pre>
IF Z = 0 THEN GOTO 11
 
LET T = N*N
=={{header|Rust}}==
GOSUB 20
<syntaxhighlight lang="rust">
GOSUB 30
fn is_prime( number : u32 ) -> bool {
IF Z = 0 THEN GOTO 11
if PRINTnumber N< 2 {
11 IF N = 31 THEN ENDfalse
}
LET N = N + 1
else GOTO 10{
let limit : u32 = (number as f32).sqrt( ).floor( ) as u32 ;
20 LET D = 0
let mut nums : Vec<u32> = Vec::new( ) ;
21 IF T = 0 THEN RETURN
LET D =for Di +in 2..=limit (T-(T/10)*10){
LET T = T/10 nums.push( i ) ;
GOTO 21 }
nums.iter( ).filter( | n | number % *n == 0 ).count( ) == 0
30 LET Z = 0
}
IF D < 2 THEN RETURN
}
LET Z = 1
 
IF D < 4 THEN RETURN
fn to_digits( mut number : u32 ) -> Vec<u32> {
LET Z = 0
let mut digits : Vec<u32> = Vec::new( ) ;
IF (D/2)*2 = D THEN RETURN
while LET Tnumber != 10 {
let remainder : u32 = number % 10 ;
31 LET T = T + 2
IF T*T>D THENdigits.push( GOTOremainder 32) ;
IF (D number /T)*T=D THEN10 RETURN;
GOTO 31}
digits
32 LET Z = 1
}
RETURN</lang>
 
{{out}}<pre>
fn digit_sum( number : u32 ) -> u32 {
16
let digits : Vec<u32> = to_digits( number ) ;
17
digits.iter( ).sum( )
25
}
28</pre>
 
fn main() {
let mut solution : Vec<u32> = Vec::new( ) ;
for i in 2..=100 {
let square = i * i ;
let cube = square * i ;
if is_prime( digit_sum( square ) ) && is_prime( digit_sum(cube ) ) {
solution.push( i ) ;
}
}
println!("{:?}" , solution);
}</syntaxhighlight>
{{out}}
<pre>
[16, 17, 25, 28, 34, 37, 47, 52, 64]
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">1..99 -> grep { .square.digits_sum.is_prime && .cube.digits_sum.is_prime }.say</syntaxhighlight>
{{out}}
<pre>
[16, 17, 25, 28, 34, 37, 47, 52, 64]
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">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()</langsyntaxhighlight>
 
{{out}}
Line 486 ⟶ 1,083:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 511 ⟶ 1,108:
if IsPrime(SumDigits(N*N)) & IsPrime(SumDigits(N*N*N)) then
[IntOut(0, N); ChOut(0, ^ )];
]</langsyntaxhighlight>
 
{{out}}
9,476

edits