Nice primes: Difference between revisions

11,150 bytes added ,  2 months ago
Task in PHP
(Task in PHP)
(23 intermediate revisions by 14 users not shown)
Line 27:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 42:
L(n) 501..999
I is_prime(digital_root(n)) & is_prime(n)
print(n, end' ‘ ’)</langsyntaxhighlight>
 
{{out}}
Line 51:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE Func IsNicePrime(INT i BYTE ARRAY primes)
Line 90:
OD
PrintF("%E%EThere are %I nice primes",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Nice_primes.png Screenshot from Atari 8-bit computer]
Line 101:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find nice primes - primes whose digital root is also prime #
INT min prime = 501;
INT max prime = 999;
Line 131:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 140:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % find some nice primes - primes whose digital root is prime %
% returns the digital root of n in base 10 %
integer procedure digitalRoot( integer value n ) ;
Line 183:
end
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 224:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">(⊢(/⍨)(∧/(2=(0+.=⍳|⊢))¨∘(⊢,(+/10⊥⍣¯1⊢)⍣(9≥⊣)))¨) 500+⍳500</langsyntaxhighlight>
{{out}}
<pre>509 547 563 569 587 599 601 617 619 641 653 659 673 677 691 709 727 743 761 797 821 839 853 857 887 907 911 929 941 947
Line 231:
=={{header|AppleScript}}==
sumn formula borrowed from the [https://www.rosettacode.org/wiki/Nice_primes#Factor Factor] solution.
<langsyntaxhighlight lang="applescript">on sieveOfEratosthenes(limit)
script o
property numberList : {missing value}
Line 272:
end nicePrimes
 
return nicePrimes(501, 999)</langsyntaxhighlight>
{{output}}
<langsyntaxhighlight lang="applescript">{509, 547, 563, 569, 587, 599, 601, 617, 619, 641, 653, 659, 673, 677, 691, 709, 727, 743, 761, 797, 821, 839, 853, 857, 887, 907, 911, 929, 941, 947, 977, 983, 997}</langsyntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">sumd: function [n][
 
<lang rebol>sumd: function [n][
s: sum digits n
(1 = size digits s)? -> return s
Line 288 ⟶ 287:
 
loop split.every:10 select 500..1000 => nice? 'a ->
print map a => [pad to :string & 4]</langsyntaxhighlight>
 
{{out}}
Line 298 ⟶ 297:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NICE_PRIMES.AWK
BEGIN {
Line 337 ⟶ 336:
return(sum)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 377 ⟶ 376:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z: B=500: E=1000
20 DIM P(E): P(0)=-1: P(1)=-1
30 FOR I=2 TO SQR(E)
Line 387 ⟶ 386:
90 IF J>0 THEN S=S+J MOD 10: J=J\10: GOTO 90
100 IF S>9 THEN J=S: GOTO 80 ELSE IF NOT P(S) THEN PRINT I,
110 NEXT</langsyntaxhighlight>
{{out}}
<pre> 509 547 563 569 587
Line 398 ⟶ 397:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
manifest $(
begin = 500
Line 434 ⟶ 433:
writef("%N*N", i)
freevec(prime)
$)</langsyntaxhighlight>
 
{{out}}
Line 470 ⟶ 469:
983
997</pre>
 
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
Line 523:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Nice primes between 500 and 1000:
Line 533:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
 
bool is_prime(unsigned int n) {
Line 567:
}
std::cout << '\n' << count << " nice primes found.\n";
}</langsyntaxhighlight>
 
{{out}}
Line 581:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.stdio;
 
bool isPrime(uint n) {
Line 627:
writeln;
writeln(count, " nice primes found.");
}</langsyntaxhighlight>
{{out}}
<pre>Nice primes between 500 and 1000:
Line 635:
977 983 997
33 nice primes found.</pre>
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
 
function SumDigits(N: integer): integer;
{Sum the integers in a number}
var T: integer;
begin
Result:=0;
repeat
begin
T:=N mod 10;
N:=N div 10;
Result:=Result+T;
end
until N<1;
end;
 
 
 
function IsNiceNumber(N: integer): boolean;
{Return True if N is a nice number}
var Sum: integer;
begin
Result:=False;
{N must be primes}
if not IsPrime(N) then exit;
{Keep summing until one digit number}
Sum:=N;
repeat Sum:=SumDigits(Sum)
until Sum<10;
{Must be prime too}
Result:=IsPrime(Sum);
end;
 
 
procedure ShowNicePrimes(Memo: TMemo);
{Display Nice Primes between 501 and 999}
var I,Cnt: integer;
var S: string;
begin
Cnt:=0; S:='';
for I:=501 to 999 do
if IsNiceNumber(I) then
begin
S:=S+Format('%4d',[i]);
Inc(Cnt);
if (Cnt mod 5)=0 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add(Format('Nice Primes: %3D',[Cnt]));
Memo.Lines.Add(S);
end;
 
</syntaxhighlight>
{{out}}
<pre>
Nice Primes: 33
509 547 563 569 587
599 601 617 619 641
653 659 673 677 691
709 727 743 761 797
821 839 853 857 887
907 911 929 941 947
977 983 997
 
</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">
// Nice primes. Nigel Galloway: March 22nd., 2021
let fN g=1+((g-1)%9) in primes32()|>Seq.skipWhile((>)500)|>Seq.takeWhile((>)1000)|>Seq.filter(fN>>isPrime)|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 658 ⟶ 752:
({{math|<var>n</var> &#61; 0}} may not need to be special-cased depending on the behavior of your language's modulo operator.)
 
<langsyntaxhighlight lang="factor">USING: math math.primes prettyprint sequences ;
 
: digital-root ( m -- n ) 1 - 9 mod 1 + ;
 
500 1000 primes-between [ digital-root prime? ] filter .</langsyntaxhighlight>
{{out}}
<pre style="height:10em">
Line 704 ⟶ 798:
{{trans|Factor}}
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 741 ⟶ 835:
 
1000 500 print_nice_primes
bye</langsyntaxhighlight>
 
{{out}}
Line 752 ⟶ 846:
33 nice primes found.
</pre>
 
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Function isPrime(Byval ValorEval As Integer) As Boolean
If ValorEval <= 1 Then Return False
Line 788 ⟶ 881:
Print !"\n\n"; column; " buenos n£meros encontrados."
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 804 ⟶ 897:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Nice_primes}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Nice primes 01.png]]
In '''[https://formulae.org/?example=Nice_primes this]''' page you can see the program(s) related to this task and their results.
 
'''Test case'''
 
[[File:Fōrmulæ - Nice primes 02.png]]
 
[[File:Fōrmulæ - Nice primes 03.png]]
 
'''Showing nice primes in the range 500 .. 1,000'''
 
[[File:Fōrmulæ - Nice primes 04.png]]
 
[[File:Fōrmulæ - Nice primes 05.png]]
 
[[File:Fōrmulæ - Nice primes 06.png]]
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 864 ⟶ 971:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 871 ⟶ 978:
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">
import Data.Char ( digitToInt )
 
isPrime :: Int -> Bool
isPrime n
|n == 2 = True
|n == 1 = False
|otherwise = null $ filter (\i -> mod n i == 0 ) [2 .. root]
where
root :: Int
root = floor $ sqrt $ fromIntegral n
 
digitsum :: Int -> Int
digitsum n = sum $ map digitToInt $ show n
 
findSumn :: Int -> Int
findSumn n = until ( (== 1) . length . show ) digitsum n
 
isNicePrime :: Int -> Bool
isNicePrime n = isPrime n && isPrime ( findSumn n )
 
solution :: [Int]
solution = filter isNicePrime [501..999]</syntaxhighlight>
{{out}}
<pre>
[509,547,563,569,587,599,601,617,619,641,653,659,673,677,691,709,727,743,761,797,821,839,853,857,887,907,911,929,941,947,977,983,997]
</pre>
 
=={{header|J}}==
Line 890 ⟶ 1,025:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">public class NicePrimes {
private static boolean isPrime(long n) {
if (n < 2) {
Line 946 ⟶ 1,081:
System.out.printf("%d nice primes found.%n", count);
}
}</langsyntaxhighlight>
{{out}}
<pre>Nice primes between 500 and 1000
Line 961 ⟶ 1,096:
This entry uses `is_prime` as defined at
[[Erd%C5%91s-primes#jq]].
<langsyntaxhighlight lang="jq">def is_nice:
# input: a non-negative integer
def sumn:
Line 973 ⟶ 1,108:
 
# The task:
range(501; 1000) | select(is_nice)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,010 ⟶ 1,145:
997
</pre>
 
=={{header|Julia}}==
See [[Strange_numbers#Julia]] for the filter_open_interval function.
<langsyntaxhighlight lang="julia">using Primes
 
isnice(n, base=10) = isprime(n) && (mod1(n - 1, base - 1) + 1) in [2, 3, 5, 7, 11, 13, 17, 19]
 
filter_open_interval(500, 1000, isnice)
</langsyntaxhighlight>{{out}}
<pre>
Finding numbers matching isnice for open interval (500, 1000):
Line 1,029 ⟶ 1,165:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">fun isPrime(n: Long): Boolean {
if (n < 2) {
return false
Line 1,083 ⟶ 1,219:
println()
println("$count nice primes found.")
}</langsyntaxhighlight>
{{out}}
<pre>Nice primes between 500 and 1000:
Line 1,094 ⟶ 1,230:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function isPrime(n)
if n < 2 then
return false
Line 1,144 ⟶ 1,280:
n = n + 1
end
print(count .. " nice primes found.")</langsyntaxhighlight>
{{out}}
<pre>Nice primes between 500 and 1000
Line 1,153 ⟶ 1,289:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[summ]
summ[n_] := FixedPoint[IntegerDigits /* Total, n]
Select[Range[501, 999], PrimeQ[#] \[And] PrimeQ[summ[#]] &]</langsyntaxhighlight>
{{out}}
<pre>{509, 547, 563, 569, 587, 599, 601, 617, 619, 641, 653, 659, 673, 677, 691, 709, 727, 743, 761, 797, 821, 839, 853, 857, 887, 907, 911, 929, 941, 947, 977, 983, 997}</pre>
 
=={{header|PARI/GP}}==
<lang PARI/GP>nicePrimes( s, e ) = { local( m );
forprime( p = s, e,
m = p; \\
while( m > 9, \\ m == m mod 9
m = sumdigits( m ) ); \\
if( isprime( m ),
print1( i, " " ) ) );
}</lang>
or
<lang PARI/GP>apply( s -> s, select( s -> isprime( s % 9 ), primes( [500, 1000] )))</lang>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, sugar
 
func isPrime(n: Positive): bool =
Line 1,202 ⟶ 1,326:
for i, n in list:
stdout.write n, if (i + 1) mod 10 == 0: '\n' else: ' '
echo()</langsyntaxhighlight>
 
{{out}}
Line 1,210 ⟶ 1,334:
821 839 853 857 887 907 911 929 941 947
977 983 997 </pre>
 
=={{header|OCaml}}==
After ruling out all multiples of three, <code>mod 9</code> (the digital root) can only return {1, 2, 4, 5, 7, 8}. Adding 6 before calculating <code>mod 9</code> makes all primes in the result even (and the composites odd), so <code>(n + 6) mod 9 land 1 = 0</code> is sufficient for checking the digital root.
<syntaxhighlight lang="ocaml">let is_nice_prime n =
let rec test x =
x * x > n || n mod x <> 0 && 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 && (n + 6) mod 9 land 1 = 0 && test 5
 
let () =
Seq.(ints 500 |> take 500 |> filter is_nice_prime |> iter (Printf.printf " %u"))
|> print_newline</syntaxhighlight>
{{out}}
<pre> 509 547 563 569 587 599 601 617 619 641 653 659 673 677 691 709 727 743 761 797 821 839 853 857 887 907 911 929 941 947 977 983 997</pre>
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">/* REXX */
n=1000
prime = .Array~new(n)~fill(.true)~~remove(1)
p.=0
Do i = 2 to n
If prime[i] = .true Then Do
Do j = i * i to n by i
prime~remove(j)
End
p.i=1
End
End
z=0
ol=''
Do i=500 To 1000
If p.i then Do
dr=digroot(i)
If p.dr Then Do
ol=ol' 'i'('dr')'
z=z+1
If z//10=0 Then Do
Say strip(ol)
ol=''
End
End
End
End
Say strip(ol)
Say z 'nice primes in the range 500 to 1000'
Exit
 
digroot:
Parse Arg s
Do Until length(s)=1
dr=0
Do j=1 To length(s)
dr=dr+substr(s,j,1)
End
s=dr
End
Return s</syntaxhighlight>
{{out}}
<pre>509(5) 547(7) 563(5) 569(2) 587(2) 599(5) 601(7) 617(5) 619(7) 641(2)
653(5) 659(2) 673(7) 677(2) 691(7) 709(7) 727(7) 743(5) 761(5) 797(5)
821(2) 839(2) 853(7) 857(2) 887(5) 907(7) 911(2) 929(2) 941(5) 947(2)
977(5) 983(2) 997(7)
33 nice primes in the range 500 to 1000</pre>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="python">nicePrimes( s, e ) = { local( m );
forprime( p = s, e,
m = p; \\
while( m > 9, \\ m == p mod 9
m = sumdigits( m ) ); \\
if( isprime( m ),
print1( p, " " ) ) );
}</syntaxhighlight>
or
<syntaxhighlight lang="pari/gp">select( p -> isprime( p % 9 ), primes( [500, 1000] ))</syntaxhighlight>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 1,230 ⟶ 1,431:
$cnt = @nice_primes;
print "Nice primes between $low and $high (total of $cnt):\n" .
(sprintf "@{['%5d' x $cnt]}", @nice_primes[0..$cnt-1]) =~ s/(.{55})/$1\n/gr;</langsyntaxhighlight>
{{out}}
<pre>Nice primes between 500 and 1000 (total of 33):
Line 1,239 ⟶ 1,440:
=={{header|Phix}}==
{{trans|Factor}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">pdr</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: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</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;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">500</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pdr</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d nice primes found:\n %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,250 ⟶ 1,451:
659 673 677 691 709 727 743 761 797 821 839
853 857 887 907 911 929 941 947 977 983 997
</pre>
 
=={{header|PHP}}==
{{trans|Python}}
<syntaxhighlight lang="php">
<?php
// Function to check if a number is prime
function isPrime($n) {
if ($n <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($n); $i++) {
if ($n % $i == 0) {
return false;
}
}
return true;
}
 
// Function to sum the digits of a number until the sum is a single digit
function sumOfDigits($n) {
while ($n > 9) {
$sum = 0;
while ($n > 0) {
$sum += $n % 10;
$n = (int)($n / 10);
}
$n = $sum;
}
return $n;
}
 
function findNicePrimes($lower_limit=501, $upper_limit=1000) {
// Find all Nice primes within the specified range
$nice_primes = array();
for ($n = $lower_limit; $n < $upper_limit; $n++) {
if (isPrime($n)) {
$sumn = sumOfDigits($n);
if (isPrime($sumn)) {
array_push($nice_primes, $n);
}
}
}
return $nice_primes;
}
// Main loop to find and print "Nice Primes"
$nice_primes = findNicePrimes();
foreach ($nice_primes as $prime) {
echo $prime . " ";
}
?>
</syntaxhighlight>
{{out}}
<pre>
509 547 563 569 587 599 601 617 619 641 653 659 673 677 691 709 727 743 761 797 821 839 853 857 887 907 911 929 941 947 977 983 997
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
def is_prime(n):
"""Check if a number is prime."""
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
 
def sum_of_digits(n):
"""Calculate the repeated sum of digits until the sum's length is 1."""
while n > 9:
n = sum(int(digit) for digit in str(n))
return n
 
def find_nice_primes(lower_limit=501, upper_limit=1000):
"""Find all Nice primes within the specified range."""
nice_primes = []
for n in range(lower_limit, upper_limit):
if is_prime(n):
sumn = sum_of_digits(n)
if is_prime(sumn):
nice_primes.append(n)
return nice_primes
 
# Example usage
nice_primes = find_nice_primes()
print(nice_primes)
</syntaxhighlight>
{{out}}
<pre>
[509, 547, 563, 569, 587, 599, 601, 617, 619, 641, 653, 659, 673, 677, 691, 709, 727, 743, 761, 797, 821, 839, 853, 857, 887, 907, 911, 929, 941, 947, 977, 983, 997]
</pre>
=={{header|PL/0}}==
<syntaxhighlight lang="pascal">
var n, sum, prime, i;
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 := 500;
while i < 999 do begin
i := i + 1;
n := i;
call isnprime;
if prime = 1 then begin
sum := n;
while sum > 9 do begin
call sumdigitsofn;
n := sum
end;
if sum = 2 then ! i;
if sum = 3 then ! i;
if sum = 5 then ! i;
if sum = 7 then ! i
end
end
end.
</syntaxhighlight>
{{out}}
Note: PL/0 can only output one value per line, to avoid a long output, the results have been manually combined to 7 per line.
<pre>
509 547 563 569 587 599 601
617 619 641 653 659 673 677
691 709 727 743 761 797 821
839 853 857 887 907 911 929
941 947 977 983 997
</pre>
 
=={{header|PL/M}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="plm">100H: /* FIND NICE PRIMES - PRIMES WHOSE DIGITAL ROOT IS ALSO PRIME */
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
DECLARE FN BYTE, ARG ADDRESS;
Line 1,333 ⟶ 1,683:
END;
END;
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 1,339 ⟶ 1,689:
673(7) 677(2) 691(7) 709(7) 727(7) 743(5) 761(5) 797(5) 821(2) 839(2) 853(7) 857(2)
887(5) 907(7) 911(2) 929(2) 941(5) 947(2) 977(5) 983(2) 997(7)
</pre>
 
=={{header|Quackery}}==
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
<syntaxhighlight lang="quackery"> 1000 eratosthenes
 
[ 1 - 9 mod 1+ ] is digitalroot ( n --> n )
 
[ dup digitalroot isprime
swap isprime and ] is niceprime ( n --> b )
 
500 times
[ i^ 500 + niceprime if
[ i^ 500 + echo sp ] ]</syntaxhighlight>
 
{{out}}
 
<pre>509 547 563 569 587 599 601 617 619 641 653 659 673 677 691 709 727 743 761 797 821 839 853 857 887 907 911 929 941 947 977 983 997
</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub digroot ($r) { .tail given $r, { [+] .comb } ... { .chars == 1 } }
my @is-nice = lazy (0..*).map: { .&is-prime && .&digroot.&is-prime ?? $_ !! False };
say @is-nice[500 ^..^ 1000].grep(*.so).batch(11)».fmt("%4d").join: "\n";</langsyntaxhighlight>
{{out}}
<pre> 509 547 563 569 587 599 601 617 619 641 653
Line 1,351 ⟶ 1,720:
 
Alternately, with somewhat better separation of concerns.
<syntaxhighlight lang="raku" perl6line>sub digroot ($r) { ($r, { .comb.sum } … { .chars == 1 }).tail }
sub is-nice ($_) { .is-prime && .&digroot.is-prime }
say (500 ^..^ 1000).grep( *.&is-nice ).batch(11)».fmt("%4d").join: "\n";</langsyntaxhighlight>
Same output.
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays nice primes, primes whose digital root is also prime.*/
parse arg lo hi cols . /*obtain optional argument from the CL.*/
if lo=='' | lo=="," then lo= 500 /*Not specified? Then use the default.*/
Line 1,401 ⟶ 1,770:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,416 ⟶ 1,785:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,445 ⟶ 1,814:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,484 ⟶ 1,853:
33: 997 > Σ = 7
done...
</pre>
 
=={{header|RPL}}==
≪ { } 500
'''DO'''
NEXTPRIME
'''IF''' DUP 1 - 9 MOD 1 + ISPRIME? '''THEN'''
SWAP OVER + SWAP '''END'''
'''UNTIL''' DUP 1000 ≥ '''END'''
DROP
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 509 547 563 569 587 599 601 617 619 641 653 659 673 677 691 709 727 743 761 797 821 839 853 857 887 907 911 929 941 947 977 983 997 }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
class Integer
def dig_root = (1+(self-1).remainder(9))
def nice? = prime? && dig_root.prime?
end
 
p (500..1000).select(&:nice?)
</syntaxhighlight>
{{out}}
<pre>[509, 547, 563, 569, 587, 599, 601, 617, 619, 641, 653, 659, 673, 677, 691, 709, 727, 743, 761, 797, 821, 839, 853, 857, 887, 907, 911, 929, 941, 947, 977, 983, 997]
</pre>
 
=={{header|Rust}}==
{{trans|Factor}}
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 1,509 ⟶ 1,906:
fn main() {
nice_primes(500, 1000);
}</langsyntaxhighlight>
 
{{out}}
Line 1,549 ⟶ 1,946:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: isPrime (in integer: number) is func
Line 1,578 ⟶ 1,975:
end if;
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,585 ⟶ 1,982:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func digital_root(n, base=10) {
while (n.len(base) > 1) {
n = n.sumdigits(base)
Line 1,592 ⟶ 1,989:
}
 
say primes(500, 1000).grep { digital_root(_).is_prime }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,600 ⟶ 1,997:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-traititerate}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./traititerate" for Stepped
import "./fmt" for Fmt
 
var sumDigits = Fn.new { |n|
Line 1,626 ⟶ 2,023:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,664 ⟶ 2,061:
32: 983 -> Σ = 2
33: 997 -> Σ = 7
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IntLen(N); \Return number of digits in N
int N, I;
for I:= 1 to 10 do
[N:= N/10;
if N = 0 then return I;
];
 
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 sum of digits in N
int N, Sum;
[Sum:= 0;
repeat N:= N/10;
Sum:= Sum + rem(0);
until N=0;
return Sum;
];
 
int C, N, SumN;
[C:= 0;
for N:= 501 to 999 do
if IsPrime(N) then
[SumN:= N;
repeat SumN:= SumDigits(SumN);
until IntLen(SumN) = 1;
if IsPrime(SumN) then
[IntOut(0, N);
C:= C+1;
if rem (C/10) then ChOut(0, ^ ) else CrLf(0);
];
];
]</syntaxhighlight>
 
{{out}}
<pre>
509 547 563 569 587 599 601 617 619 641
653 659 673 677 691 709 727 743 761 797
821 839 853 857 887 907 911 929 941 947
977 983 997
</pre>
3

edits