Jump to content

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

m
syntax highlighting fixup automation
m (→‎{{header|Sidef}}: less than)
m (syntax highlighting fixup automation)
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>
Line 48:
 
=={{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}}==
 
<langsyntaxhighlight lang="rebol">print select 1..100 'x ->
and? [prime? sum digits x^2]
[prime? sum digits x^3]</langsyntaxhighlight>
 
{{out}}
Line 62:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUM_OF_SQUARE_AND_CUBE_DIGITS_OF_AN_INTEGER_ARE_PRIMES.AWK
# converted from FreeBASIC
Line 96:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 104:
 
=={{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 136:
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 173:
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 241:
CHECK-DIVISOR.
DIVIDE SUM BY DIVISOR GIVING DIV-TEST.
IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.</langsyntaxhighlight>
{{out}}
<pre>16
Line 255:
=={{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>
Line 267:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight 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 .</langsyntaxhighlight>
{{out}}
<pre>
Line 276:
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 F I=1,100;D 2
01.20 Q
 
Line 297:
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 310:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
function digsum(byval n as uinteger, b as const uinteger) as uinteger
'digital sum of n in base b
Line 335:
for n as uinteger = 1 to 99
if isprime(digsum(n^3,10)) andalso isprime(digsum(n^2,10)) then print n;" ";
next n</langsyntaxhighlight>
{{out}}<pre>16 17 25 28 34 37 47 52 64</pre>
 
=={{header|Go}}==
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 357:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 365:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.Numbers.Primes (isPrime)
 
Line 387:
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|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 436:
VECTOR VALUES FMT = $I2*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre>16
Line 450:
=={{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 459:
is_prime( vecsum( split //, $_ ** 2 ) ) &&
is_prime( vecsum( split //, $_ ** 3 ) ), 1 .. 100;
print "@results\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 466:
 
=={{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>
Line 480:
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
Line 499:
if isPrime(digSum(n**3, 10)) and isPrime(digSum(n**2, 10)):
print(n, end = " ")
</syntaxhighlight>
</lang>
{{out}}
<pre>16 17 25 28 34 37 47 52 64</pre>
 
===Functional===
<langsyntaxhighlight lang="python">'''Square and cube both have prime decimal digit sums'''
 
# p :: Int -> Bool
Line 563:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[16, 17, 25, 28, 34, 37, 47, 52, 64]</pre>
Line 571:
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 swap
[ dup while
10 /mod
Line 583:
[ i^ 1+ 3 **
digitsum isprime if
[ i^ 1+ echo sp ] ] ]</langsyntaxhighlight>
 
{{out}}
Line 590:
 
=={{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 618:
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 627:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">1..99 -> grep { .square.digits_sum.is_prime && .cube.digits_sum.is_prime }.say</langsyntaxhighlight>
{{out}}
<pre>
Line 635:
=={{header|TinyBASIC}}==
This can only go up to 31 because 32^3 is too big to fit in a signed 16-bit int.
<langsyntaxhighlight lang="tinybasic">REM N, the number to be tested
REM D, the digital sum of its square or cube
REM T, temporary variable
Line 670:
GOTO 31
32 LET Z = 1
RETURN</langsyntaxhighlight>
{{out}}<pre>
16
Line 679:
=={{header|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight 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()</langsyntaxhighlight>
 
{{out}}
Line 692:
 
=={{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 717:
if IsPrime(SumDigits(N*N)) & IsPrime(SumDigits(N*N*N)) then
[IntOut(0, N); ChOut(0, ^ )];
]</langsyntaxhighlight>
 
{{out}}
Line 726:
=={{header|Yabasic}}==
{{trans|Ring}}
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Sum_of_square_and_cube_digits_of_an_integer_are_primes
// by Galileo, 04/2022
 
Line 756:
endif
next
print</langsyntaxhighlight>
{{out}}
<pre>16 17 25 28 34 37 47 52 64
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.