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

Dialects of BASIC moved to the BASIC section.
(Added Algol W)
(Dialects of BASIC moved to the BASIC section.)
Line 146:
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|Tiny BASIC}}===
This can only go up to 31 because 32^3 is too big to fit in a signed 16-bit int.
<syntaxhighlight 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</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}}==
Line 352 ⟶ 463:
= 52
= 64</pre>
 
=={{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|Go}}==
Line 806 ⟶ 888:
[16, 17, 25, 28, 34, 37, 47, 52, 64]
</pre>
 
=={{header|TinyBASIC}}==
This can only go up to 31 because 32^3 is too big to fit in a signed 16-bit int.
<syntaxhighlight 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</syntaxhighlight>
{{out}}<pre>
16
17
25
28</pre>
 
=={{header|Wren}}==
Line 897 ⟶ 935:
16 17 25 28 34 37 47 52 64
</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>
511

edits