Jump to content

Palindromic primes: Difference between revisions

m
syntax highlighting fixup automation
(Added C++ solution)
m (syntax highlighting fixup automation)
Line 7:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 22:
I s == reversed(s)
print(n, end' ‘ ’)
print()</langsyntaxhighlight>
 
{{out}}
Line 31:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE Func IsPalindromicPrime(INT i BYTE ARRAY primes)
Line 68:
OD
PrintF("%E%EThere are %I palindromic primes",count)
RETURN</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="algol68">BEGIN # find primes that are palendromic in base 10 #
INT max prime = 999;
# sieve the primes to max prime #
Line 105:
OD;
print( ( newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 113:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">loop split.every: 10 select 2..1000 'x [
and? prime? x
x = to :integer reverse to :string x
] 'a -> print map a => [pad to :string & 4]</langsyntaxhighlight>
 
{{out}}
Line 124:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PALINDROMIC_PRIMES.AWK
BEGIN {
Line 155:
return(rts)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 164:
=={{header|C++}}==
This includes a solution for the similar task [[Palindromic primes in base 16]].
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <cmath>
#include <iomanip>
Line 272:
std::cout << '\n';
print_palindromic_primes(16, 500);
}</langsyntaxhighlight>
 
{{out}}
Line 303:
A simple solution that suffices for the task:
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: kernel math.primes present prettyprint sequences ;
 
1000 primes-upto [ present dup reverse = ] filter stack.</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="factor">USING: io kernel lists lists.lazy math math.functions
math.primes math.ranges prettyprint sequences
tools.memory.private ;
Line 357:
 
"Palindromic primes less than 1,000:" print
lpalindrome-primes [ 1000 < ] lwhile [ . ] leach</langsyntaxhighlight>
{{out}}
<pre style="height:14em">
Line 387:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
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</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 447:
fmt.Println()
fmt.Println(len(bigPals), "such primes found,", len(pals), "in all.")
}</langsyntaxhighlight>
 
{{out}}
Line 455:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes
 
palindromicPrimes :: [Integer]
Line 466:
takeWhile
(1000 >)
palindromicPrimes</langsyntaxhighlight>
{{Out}}
<pre>2
Line 500:
 
'''Preliminaries'''
<langsyntaxhighlight 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;</langsyntaxhighlight>
'''Naive version'''
<syntaxhighlight lang="jq">
<lang jq>
def primes:
2, (range(3;infinite;2) | select(is_prime));
Line 510:
def palindromic_primes_slowly:
primes | select( tostring|explode | (. == reverse));
</syntaxhighlight>
</lang>
'''Less naive version'''
<langsyntaxhighlight lang="jq"># Output: an unbounded stream of palindromic primes
def palindromic_primes:
 
Line 543:
2, 3, 5, 7, 11,
(palindromic_candidates | tonumber | select(is_prime));</langsyntaxhighlight>
'''Demonstrations'''
<langsyntaxhighlight lang="jq">"Palindromic primes < 1000:",
emit_until(. >= 1000; palindromic_primes),
 
((range(5;11) | pow(10;.)) as $n
| "\nNumber of palindromic primes <= \($n): \(count(emit_until(. >= $n; palindromic_primes)))" )</langsyntaxhighlight>
{{out}}
<pre>
Line 589:
=={{header|Julia}}==
Generator method.
<langsyntaxhighlight lang="julia">using Primes
 
parray = [2, 3, 5, 7, 9, 11]
Line 596:
 
println(results)
</langsyntaxhighlight>{{out}}
<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}}==
<langsyntaxhighlight Mathematicalang="mathematica">Select[Range[999], PrimeQ[#] \[And] PalindromeQ[#] &]</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Nimlang="nim">import strutils
 
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: ' '</langsyntaxhighlight>
 
{{out}}
Line 641:
=={{header|PARI/GP}}==
'''naive'''
<langsyntaxhighlight lang="parigp">forprime(i = 2, 1000,
if( i == fromdigits( Vecrev( digits( i ) )) ,
print1( i, " " ) ) );</langsyntaxhighlight>
{{out}}
<pre>
Line 650:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Palindromic_primes
use warnings;
 
$_ == reverse and (1 x $_ ) !~ /^(11+)\1+$/ and print "$_ " for 2 .. 1e3;</langsyntaxhighlight>
{{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===
<!--<langsyntaxhighlight Phixlang="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;">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 &lt; %,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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 678:
</pre>
===filter palindromes for primality===
<!--<langsyntaxhighlight Phixlang="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: #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 &lt; %,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>
<!--</langsyntaxhighlight>-->
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.
<langsyntaxhighlight lang="python">'''Palindromic primes'''
 
from itertools import takewhile
Line 744:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>2
Line 771:
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]]
 
<langsyntaxhighlight Quackerylang="quackery"> [ [] swap
[ base share /mod
rot swap join swap
Line 784:
[ i^ isprime if
[ i^ digits palindromic if
[ i^ echo sp ] ] ]</langsyntaxhighlight>
 
{{out}}
Line 792:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>say "{+$_} matching numbers:\n{.batch(10)».fmt('%3d').join: "\n"}"
given (^1000).grep: { .is-prime and $_ eq .flip };</langsyntaxhighlight>
{{out}}
<pre>20 matching numbers:
Line 800:
 
=={{header|REXX}}==
<langsyntaxhighlight 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.*/
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 875:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
Line 906:
ok
next
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 947:
=={{header|Rust}}==
This includes a solution for the similar task [[Palindromic primes in base 16]].
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// radix_fmt = "1.0"
Line 1,028:
println!();
print_palindromic_primes(16, 500);
}</langsyntaxhighlight>
 
{{out}}
Line 1,056:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func palindromic_primes(upto, base = 10) {
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}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,089:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
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.")</langsyntaxhighlight>
 
{{out}}
Line 1,141:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
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\);
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits

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