Palindromic primes: Difference between revisions
Content added Content deleted
(Added C++ solution) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7: | Line 7: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">F is_prime(a) |
||
I a == 2 |
I a == 2 |
||
R 1B |
R 1B |
||
Line 22: | Line 22: | ||
I s == reversed(s) |
I s == reversed(s) |
||
print(n, end' ‘ ’) |
print(n, end' ‘ ’) |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 31: | Line 31: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Sieve of Eratosthenes}} |
{{libheader|Action! Sieve of Eratosthenes}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT" |
||
BYTE Func IsPalindromicPrime(INT i BYTE ARRAY primes) |
BYTE Func IsPalindromicPrime(INT i BYTE ARRAY primes) |
||
Line 68: | Line 68: | ||
OD |
OD |
||
PrintF("%E%EThere are %I palindromic primes",count) |
PrintF("%E%EThere are %I palindromic primes",count) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Palindromic_primes.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Palindromic_primes.png Screenshot from Atari 8-bit computer] |
||
Line 80: | Line 80: | ||
Generates the palindrmic 3 digit numbers and uses the observations that all 1 digit primes are palindromic and that for 2 digit numbers, only multiples of 11 are palindromic and hence 11 is the only two digit palindromic prime. |
Generates the palindrmic 3 digit numbers and uses the observations that all 1 digit primes are palindromic and that for 2 digit numbers, only multiples of 11 are palindromic and hence 11 is the only two digit palindromic prime. |
||
{{libheader|ALGOL 68-primes}} |
{{libheader|ALGOL 68-primes}} |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find primes that are palendromic in base 10 # |
||
INT max prime = 999; |
INT max prime = 999; |
||
# sieve the primes to max prime # |
# sieve the primes to max prime # |
||
Line 105: | Line 105: | ||
OD; |
OD; |
||
print( ( newline ) ) |
print( ( newline ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 113: | Line 113: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">loop split.every: 10 select 2..1000 'x [ |
||
and? prime? x |
and? prime? x |
||
x = to :integer reverse to :string x |
x = to :integer reverse to :string x |
||
] 'a -> print map a => [pad to :string & 4]</ |
] 'a -> print map a => [pad to :string & 4]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 124: | Line 124: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f PALINDROMIC_PRIMES.AWK |
# syntax: GAWK -f PALINDROMIC_PRIMES.AWK |
||
BEGIN { |
BEGIN { |
||
Line 155: | Line 155: | ||
return(rts) |
return(rts) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 164: | Line 164: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
This includes a solution for the similar task [[Palindromic primes in base 16]]. |
This includes a solution for the similar task [[Palindromic primes in base 16]]. |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <cmath> |
#include <cmath> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 272: | Line 272: | ||
std::cout << '\n'; |
std::cout << '\n'; |
||
print_palindromic_primes(16, 500); |
print_palindromic_primes(16, 500); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 303: | Line 303: | ||
A simple solution that suffices for the task: |
A simple solution that suffices for the task: |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: kernel math.primes present prettyprint sequences ; |
||
1000 primes-upto [ present dup reverse = ] filter stack.</ |
1000 primes-upto [ present dup reverse = ] filter stack.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:14em"> |
<pre style="height:14em"> |
||
Line 332: | Line 332: | ||
A much more efficient solution that generates palindromic numbers directly and filters primes from them: |
A much more efficient solution that generates palindromic numbers directly and filters primes from them: |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: io kernel lists lists.lazy math math.functions |
||
math.primes math.ranges prettyprint sequences |
math.primes math.ranges prettyprint sequences |
||
tools.memory.private ; |
tools.memory.private ; |
||
Line 357: | Line 357: | ||
"Palindromic primes less than 1,000:" print |
"Palindromic primes less than 1,000:" print |
||
lpalindrome-primes [ 1000 < ] lwhile [ . ] leach</ |
lpalindrome-primes [ 1000 < ] lwhile [ . ] leach</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:14em"> |
<pre style="height:14em"> |
||
Line 387: | Line 387: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">#include "isprime.bas" |
||
function is_pal( s as string ) as boolean |
function is_pal( s as string ) as boolean |
||
Line 399: | Line 399: | ||
for i as uinteger = 2 to 999 |
for i as uinteger = 2 to 999 |
||
if is_pal( str(i) ) andalso isprime(i) then print i;" "; |
if is_pal( str(i) ) andalso isprime(i) then print i;" "; |
||
next i : print</ |
next i : print</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929</pre> |
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929</pre> |
||
Line 406: | Line 406: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 447: | Line 447: | ||
fmt.Println() |
fmt.Println() |
||
fmt.Println(len(bigPals), "such primes found,", len(pals), "in all.") |
fmt.Println(len(bigPals), "such primes found,", len(pals), "in all.") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 455: | Line 455: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Numbers.Primes |
||
palindromicPrimes :: [Integer] |
palindromicPrimes :: [Integer] |
||
Line 466: | Line 466: | ||
takeWhile |
takeWhile |
||
(1000 >) |
(1000 >) |
||
palindromicPrimes</ |
palindromicPrimes</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2 |
<pre>2 |
||
Line 500: | Line 500: | ||
'''Preliminaries''' |
'''Preliminaries''' |
||
< |
<syntaxhighlight lang="jq">def count(s): reduce s as $x (null; .+1); |
||
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;</ |
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;</syntaxhighlight> |
||
'''Naive version''' |
'''Naive version''' |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
def primes: |
def primes: |
||
2, (range(3;infinite;2) | select(is_prime)); |
2, (range(3;infinite;2) | select(is_prime)); |
||
Line 510: | Line 510: | ||
def palindromic_primes_slowly: |
def palindromic_primes_slowly: |
||
primes | select( tostring|explode | (. == reverse)); |
primes | select( tostring|explode | (. == reverse)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Less naive version''' |
'''Less naive version''' |
||
< |
<syntaxhighlight lang="jq"># Output: an unbounded stream of palindromic primes |
||
def palindromic_primes: |
def palindromic_primes: |
||
Line 543: | Line 543: | ||
2, 3, 5, 7, 11, |
2, 3, 5, 7, 11, |
||
(palindromic_candidates | tonumber | select(is_prime));</ |
(palindromic_candidates | tonumber | select(is_prime));</syntaxhighlight> |
||
'''Demonstrations''' |
'''Demonstrations''' |
||
< |
<syntaxhighlight lang="jq">"Palindromic primes < 1000:", |
||
emit_until(. >= 1000; palindromic_primes), |
emit_until(. >= 1000; palindromic_primes), |
||
((range(5;11) | pow(10;.)) as $n |
((range(5;11) | pow(10;.)) as $n |
||
| "\nNumber of palindromic primes <= \($n): \(count(emit_until(. >= $n; palindromic_primes)))" )</ |
| "\nNumber of palindromic primes <= \($n): \(count(emit_until(. >= $n; palindromic_primes)))" )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 589: | Line 589: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Generator method. |
Generator method. |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
parray = [2, 3, 5, 7, 9, 11] |
parray = [2, 3, 5, 7, 9, 11] |
||
Line 596: | Line 596: | ||
println(results) |
println(results) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre>[2, 3, 5, 7, 9, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929]</pre> |
<pre>[2, 3, 5, 7, 9, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929]</pre> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Select[Range[999], PrimeQ[#] \[And] PalindromeQ[#] &]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{2, 3, 5, 7, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929}</pre> |
<pre>{2, 3, 5, 7, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929}</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strutils |
||
const N = 999 |
const N = 999 |
||
Line 633: | Line 633: | ||
for i, n in result: |
for i, n in result: |
||
stdout.write ($n).align(3) |
stdout.write ($n).align(3) |
||
stdout.write if (i + 1) mod 10 == 0: '\n' else: ' '</ |
stdout.write if (i + 1) mod 10 == 0: '\n' else: ' '</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 641: | Line 641: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
'''naive''' |
'''naive''' |
||
< |
<syntaxhighlight lang="parigp">forprime(i = 2, 1000, |
||
if( i == fromdigits( Vecrev( digits( i ) )) , |
if( i == fromdigits( Vecrev( digits( i ) )) , |
||
print1( i, " " ) ) );</ |
print1( i, " " ) ) );</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 650: | Line 650: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Palindromic_primes |
use strict; # https://rosettacode.org/wiki/Palindromic_primes |
||
use warnings; |
use warnings; |
||
$_ == reverse and (1 x $_ ) !~ /^(11+)\1+$/ and print "$_ " for 2 .. 1e3;</ |
$_ == reverse and (1 x $_ ) !~ /^(11+)\1+$/ and print "$_ " for 2 .. 1e3;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929</pre> |
<pre>2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929</pre> |
||
Line 661: | Line 661: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
===filter primes for palindromicness=== |
===filter primes for palindromicness=== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">palindrome</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</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;">palindrome</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
||
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span> |
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span> |
||
Line 671: | Line 671: | ||
<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;">"found %d < %,d: %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: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</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;">"found %d < %,d: %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: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 678: | Line 678: | ||
</pre> |
</pre> |
||
===filter palindromes for primality=== |
===filter palindromes for primality=== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
||
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span> |
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span> |
||
Line 692: | Line 692: | ||
<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;">"found %d < %,d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">s</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;">"found %d < %,d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
Same output. Didn't actually test if this way was any faster, but expect it would be. |
Same output. Didn't actually test if this way was any faster, but expect it would be. |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
A non-finite generator of palindromic primes – one of many approaches to solving this problem in Python. |
A non-finite generator of palindromic primes – one of many approaches to solving this problem in Python. |
||
< |
<syntaxhighlight lang="python">'''Palindromic primes''' |
||
from itertools import takewhile |
from itertools import takewhile |
||
Line 744: | Line 744: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main() |
main() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>2 |
<pre>2 |
||
Line 771: | Line 771: | ||
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]] |
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]] |
||
< |
<syntaxhighlight lang="quackery"> [ [] swap |
||
[ base share /mod |
[ base share /mod |
||
rot swap join swap |
rot swap join swap |
||
Line 784: | Line 784: | ||
[ i^ isprime if |
[ i^ isprime if |
||
[ i^ digits palindromic if |
[ i^ digits palindromic if |
||
[ i^ echo sp ] ] ]</ |
[ i^ echo sp ] ] ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 792: | Line 792: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>say "{+$_} matching numbers:\n{.batch(10)».fmt('%3d').join: "\n"}" |
||
given (^1000).grep: { .is-prime and $_ eq .flip };</ |
given (^1000).grep: { .is-prime and $_ eq .flip };</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>20 matching numbers: |
<pre>20 matching numbers: |
||
Line 800: | Line 800: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds and displays palindromic primes in base ten for all N < 1,000.*/ |
||
parse arg hi cols . /*obtain optional argument from the CL.*/ |
parse arg hi cols . /*obtain optional argument from the CL.*/ |
||
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ |
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ |
||
Line 841: | Line 841: | ||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
||
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
||
end /*j*/; return</ |
end /*j*/; return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 875: | Line 875: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 906: | Line 906: | ||
ok |
ok |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 947: | Line 947: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
This includes a solution for the similar task [[Palindromic primes in base 16]]. |
This includes a solution for the similar task [[Palindromic primes in base 16]]. |
||
< |
<syntaxhighlight lang="rust">// [dependencies] |
||
// primal = "0.3" |
// primal = "0.3" |
||
// radix_fmt = "1.0" |
// radix_fmt = "1.0" |
||
Line 1,028: | Line 1,028: | ||
println!(); |
println!(); |
||
print_palindromic_primes(16, 500); |
print_palindromic_primes(16, 500); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,056: | Line 1,056: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func palindromic_primes(upto, base = 10) { |
||
var list = [] |
var list = [] |
||
for (var p = 2; p <= upto; p = p.next_palindrome(base)) { |
for (var p = 2; p <= upto; p = p.next_palindrome(base)) { |
||
Line 1,069: | Line 1,069: | ||
var count = palindromic_primes(10**n).len |
var count = palindromic_primes(10**n).len |
||
say "There are #{count} palindromic primes <= 10^#{n}" |
say "There are #{count} palindromic primes <= 10^#{n}" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,089: | Line 1,089: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
import "/seq" for Lst |
import "/seq" for Lst |
||
Line 1,115: | Line 1,115: | ||
var bigPals = pals.where { |p| p >= 1000 }.toList |
var bigPals = pals.where { |p| p >= 1000 }.toList |
||
for (chunk in Lst.chunks(bigPals, 10)) Fmt.print("$,6d", chunk) |
for (chunk in Lst.chunks(bigPals, 10)) Fmt.print("$,6d", chunk) |
||
System.print("\n%(bigPals.count) such primes found, %(pals.count) in all.")</ |
System.print("\n%(bigPals.count) such primes found, %(pals.count) in all.")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,141: | Line 1,141: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number |
||
int N, I; |
int N, I; |
||
[if N <= 1 then return false; |
[if N <= 1 then return false; |
||
Line 1,166: | Line 1,166: | ||
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\); |
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\); |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |