Sum of square and cube digits of an integer are primes: Difference between revisions
Not a robot (talk | contribs) Add APL |
|||
Line 40:
16 17 25 28 34 37 47 52 64
</pre>
=={{header|APL}}==
<lang apl>(⊢(/⍨)∧/∘((2=0+.=⍳|⊢)∘(+/⍎¨∘⍕)¨*∘2 3)¨)⍳100</lang>
{{out}}
<pre>16 17 25 28 34 37 47 52 64</pre>
=={{header|F_Sharp|F#}}==
|
Revision as of 11:54, 2 January 2022
- Task
Find and show here all positive integers n less than 100 where:
- the sum of the digits of the square of n is prime; and
- the sum of the digits of the cube of n is also prime.
- Example
16 satisfies the task descrption because 16 x 16 = 256 has a digit sum of 13 which is prime and
16 x 16 x 16 = 4096 has a digit sum of 19 which is also prime.
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
APL
<lang apl>(⊢(/⍨)∧/∘((2=0+.=⍳|⊢)∘(+/⍎¨∘⍕)¨*∘2 3)¨)⍳100</lang>
- Output:
16 17 25 28 34 37 47 52 64
F#
This task uses Extensible Prime Generator (F#) <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 "" </lang>
- Output:
16 17 25 28 34 37 47 52 64
FreeBASIC
<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</lang>
- Output:
16 17 25 28 34 37 47 52 64
Haskell
<lang haskell>import Data.Bifunctor (first) import Data.Numbers.Primes (isPrime)
p :: Int -> Bool p =
((&&) . primeDigitSum . (^ 2)) <*> (primeDigitSum . (^ 3))
TEST -------------------------
main :: IO () main = print $ filter p [2 .. 99]
GENERIC ------------------------
primeDigitSum :: Int -> Bool primeDigitSum = isPrime . digitSum 10
digitSum :: Int -> Int -> Int digitSum base = go
where go 0 = 0 go n = uncurry (+) (first go $ quotRem n base)</lang>
- Output:
[16,17,25,28,34,37,47,52,64]
Julia
<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] </lang>
Perl
<lang perl>#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Sum_of_square_and_cube_digits_of_an_integer_are_primes use warnings; use ntheory qw( is_prime vecsum );
my @results = grep
is_prime( vecsum( split //, $_ ** 2 ) ) && is_prime( vecsum( split //, $_ ** 3 ) ), 1 .. 100;
print "@results\n";</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...
TinyBASIC
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
LET N = 1 10 LET T = N*N*N GOSUB 20 GOSUB 30 IF Z = 0 THEN GOTO 11 LET T = N*N GOSUB 20 GOSUB 30 IF Z = 0 THEN GOTO 11 PRINT N 11 IF N = 31 THEN END LET N = N + 1 GOTO 10 20 LET D = 0 21 IF T = 0 THEN RETURN LET D = D + (T-(T/10)*10) LET T = T/10 GOTO 21 30 LET Z = 0 IF D < 2 THEN RETURN LET Z = 1 IF D < 4 THEN RETURN LET Z = 0 IF (D/2)*2 = D THEN RETURN LET T = 1 31 LET T = T + 2 IF T*T>D THEN GOTO 32 IF (D/T)*T=D THEN RETURN GOTO 31 32 LET Z = 1 RETURN</lang>
- Output:
16 17 25
28
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