Nice primes: Difference between revisions

6,621 bytes added ,  2 months ago
Task in PHP
No edit summary
(Task in PHP)
(12 intermediate revisions by 8 users not shown)
Line 277:
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">sumd: function [n][
s: sum digits n
Line 470 ⟶ 469:
983
997</pre>
 
 
=={{header|C}}==
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#}}==
Line 752 ⟶ 846:
33 nice primes found.
</pre>
 
 
=={{header|FreeBASIC}}==
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}}==
Line 899 ⟶ 1,006:
[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 1,039 ⟶ 1,145:
997
</pre>
 
=={{header|Julia}}==
See [[Strange_numbers#Julia]] for the filter_open_interval function.
Line 1,227 ⟶ 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}}==
Line 1,328 ⟶ 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>
 
Line 1,420 ⟶ 1,692:
 
=={{header|Quackery}}==
 
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
Line 1,582 ⟶ 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>
 
Line 1,698 ⟶ 1,997:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-traititerate}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./traititerate" for Stepped
import "./fmt" for Fmt
 
var sumDigits = Fn.new { |n|
3

edits