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

m
→‎{{header|Wren}}: Changed to Wren S/H
(Dialects of BASIC moved to the BASIC section.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(8 intermediate revisions by 5 users not shown)
Line 176:
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="tinybasicbasic">10 REM Sum of square and cube N,digits theof numberan tointeger beare testedprimes
20 REM DN, the digital sum of itsnumber squareto orbe cubetested
30 REM D, the digital sum of its square or cube
REM T, temporary variable
40 REM T, temporary variable
REM Z, did D test as prime or not
50 REM Z, did D test as prime or not
 
60 LET N = 1
1070 LET T = N * N * N
80 GOSUB 20200
90 GOSUB 30260
100 IF Z = 0 THEN GOTO 11160
110 LET T = N * N
120 GOSUB 20200
130 GOSUB 30260
140 IF Z = 0 THEN GOTO 11160
150 PRINT N
11160 IF N = 31 THEN END
170 LET N = N + 1
180 GOTO 1070
190 REM Calculate sum of digits
20 LET D = 0
21200 IFLET TD = 0 THEN RETURN
210 IF T = 0 THEN RETURN
LET D = D + (T-(T/10)*10)
220 LET D = LETD + (T =- (T / 10) * 10)
230 LET T = GOTOT 21/ 10
240 GOTO 210
30 LET Z = 0
250 REM Check if is prime
IF D < 2 THEN RETURN
260 LET Z = 10
270 IF D < 42 THEN RETURN
280 LET Z = 01
290 IF (D/2)*2 =< D4 THEN RETURN
300 LET TZ = 10
310 IF (D / 2) * 2 = D THEN RETURN
31 LET T = T + 2
320 LET T = 1
IF T*T>D THEN GOTO 32
330 LET T = T + 2
IF (D/T)*T=D THEN RETURN
340 IF T * T > D THEN GOTO 31370
350 IF (D / T) * T = D THEN RETURN
32 LET Z = 1
360 GOTO 330
RETURN</syntaxhighlight>
370 LET Z = 1
{{out}}<pre>
380 RETURN</syntaxhighlight>
16
{{out}}<pre>16
17
25
Line 407 ⟶ 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#}}==
Line 622 ⟶ 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}}==
Line 880 ⟶ 1,003:
16 17 25 28 34 37 47 52 64
done...
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
p (1..100).select{|n|(n*n).digits.sum.prime? && (n**3).digits.sum.prime?}</syntaxhighlight>
{{out}}
<pre>
[16, 17, 25, 28, 34, 37, 47, 52, 64]
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
fn is_prime( number : u32 ) -> bool {
if number < 2 {
false
}
else {
let limit : u32 = (number as f32).sqrt( ).floor( ) as u32 ;
let mut nums : Vec<u32> = Vec::new( ) ;
for i in 2..=limit {
nums.push( i ) ;
}
nums.iter( ).filter( | n | number % *n == 0 ).count( ) == 0
}
}
 
fn to_digits( mut number : u32 ) -> Vec<u32> {
let mut digits : Vec<u32> = Vec::new( ) ;
while number != 0 {
let remainder : u32 = number % 10 ;
digits.push( remainder ) ;
number /= 10 ;
}
digits
}
 
fn digit_sum( number : u32 ) -> u32 {
let digits : Vec<u32> = to_digits( number ) ;
digits.iter( ).sum( )
}
 
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>
 
Line 891 ⟶ 1,070:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
for (i in 1..99) {
9,476

edits