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