Concatenate two primes is also prime: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 8: Line 8:
{{trans|Nim}}
{{trans|Nim}}


<lang 11l>F is_prime(a)
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
I a == 2
R 1B
R 1B
Line 29: Line 29:
print(‘Found ’concatPrimes.len‘ primes which are a concatenation of two primes below 100:’)
print(‘Found ’concatPrimes.len‘ primes which are a concatenation of two primes below 100:’)
L(n) sorted(Array(concatPrimes))
L(n) sorted(Array(concatPrimes))
print(‘#4’.format(n), end' I (L.index + 1) % 16 == 0 {"\n"} E ‘ ’)</lang>
print(‘#4’.format(n), end' I (L.index + 1) % 16 == 0 {"\n"} E ‘ ’)</syntaxhighlight>


{{out}}
{{out}}
Line 47: Line 47:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
{{libheader|Action! Sieve of Eratosthenes}}
{{libheader|Action! Sieve of Eratosthenes}}
<lang Action!>INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
<syntaxhighlight lang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
INCLUDE "H6:SIEVE.ACT"
INCLUDE "H6:SIEVE.ACT"


Line 88: Line 88:
OD
OD
PrintF("%E%EThere are %I primes",count)
PrintF("%E%EThere are %I primes",count)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Concatenate_two_primes_is_also_prime.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Concatenate_two_primes_is_also_prime.png Screenshot from Atari 8-bit computer]
Line 105: Line 105:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_Io;
<syntaxhighlight lang="ada">with Ada.Text_Io;
with Ada.Integer_Text_Io;
with Ada.Integer_Text_Io;
with Ada.Strings.Fixed;
with Ada.Strings.Fixed;
Line 164: Line 164:
Put (" concat primes.");
Put (" concat primes.");
New_Line;
New_Line;
end Concat_Is_Prime;</lang>
end Concat_Is_Prime;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 185: Line 185:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find primes whose decimal representation is the concatenation of 2 primes #
<syntaxhighlight lang="algol68">BEGIN # find primes whose decimal representation is the concatenation of 2 primes #
INT max low prime = 99; # for the task, only need component primes up to 99 #
INT max low prime = 99; # for the task, only need component primes up to 99 #
INT max prime = max low prime * max low prime;
INT max prime = max low prime * max low prime;
Line 214: Line 214:
OD;
OD;
print( ( newline, newline, "Found ", whole( c count, 0 ), " concat primes", newline ) )
print( ( newline, newline, "Found ", whole( c count, 0 ), " concat primes", newline ) )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 236: Line 236:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
The Algol W for loop allows the loop counter values to be specified as a list - as there are only 25 primes below 100, this feature is used here to save looking through the sieve for the low primes.
The Algol W for loop allows the loop counter values to be specified as a list - as there are only 25 primes below 100, this feature is used here to save looking through the sieve for the low primes.
<lang algolw>begin % find primes whose decimal representation is the concatenation of 2 primes %
<syntaxhighlight lang="algolw">begin % find primes whose decimal representation is the concatenation of 2 primes %
integer MAX_PRIME;
integer MAX_PRIME;
MAX_PRIME := 99 * 99;
MAX_PRIME := 99 * 99;
Line 274: Line 274:
write();write( i_w := 1, s_w := 0, "Found ", cCount, " concat primes" )
write();write( i_w := 1, s_w := 0, "Found ", cCount, " concat primes" )
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 294: Line 294:
</pre>
</pre>
=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f CONCATENATE_TWO_PRIMES_IS_ALSO_PRIME.AWK
# syntax: GAWK -f CONCATENATE_TWO_PRIMES_IS_ALSO_PRIME.AWK
#
#
Line 333: Line 333:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 354: Line 354:
=={{header|BASIC}}==
=={{header|BASIC}}==
==={{header|BASIC256}}===
==={{header|BASIC256}}===
<lang BASIC256>c = 0
<syntaxhighlight lang="basic256">c = 0
for p1 = 2 to 99
for p1 = 2 to 99
if not isPrime(p1) then continue for
if not isPrime(p1) then continue for
Line 378: Line 378:
end while
end while
return True
return True
end function</lang>
end function</syntaxhighlight>


==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
This solution focuses more on the primes p1, p2 than on the concatenated prime. Thus, there can be multiple solutions. For example, 373 can be formed from 37 and 3 or from 3 and 73 and will be listed twice.
This solution focuses more on the primes p1, p2 than on the concatenated prime. Thus, there can be multiple solutions. For example, 373 can be formed from 37 and 3 or from 3 and 73 and will be listed twice.
<lang freebasic>#include "isprime.bas"
<syntaxhighlight lang="freebasic">#include "isprime.bas"
dim as integer p1, p2, cat, c = 0
dim as integer p1, p2, cat, c = 0


Line 397: Line 397:
next p2
next p2
next p1
next p1
</syntaxhighlight>
</lang>
{{out}}<pre>
{{out}}<pre>
2| 3 2|11 2|23 2|29 2|41 2|71 2|83 3| 7 3|11 3|13
2| 3 2|11 2|23 2|29 2|41 2|71 2|83 3| 7 3|11 3|13
Line 416: Line 416:


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang yabasic>c = 0
<syntaxhighlight lang="yabasic">c = 0
for p1 = 2 to 99
for p1 = 2 to 99
if not isPrime(p1) then continue : fi
if not isPrime(p1) then continue : fi
Line 443: Line 443:
wend
wend
return True
return True
end sub</lang>
end sub</syntaxhighlight>




=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: formatting grouping io kernel math.parser math.primes
<syntaxhighlight lang="factor">USING: formatting grouping io kernel math.parser math.primes
present prettyprint sequences sets sorting ;
present prettyprint sequences sets sorting ;


Line 454: Line 454:
99 primes-upto [ present ] map dup [ append dec> ] cartesian-map
99 primes-upto [ present ] map dup [ append dec> ] cartesian-map
concat [ prime? ] filter members natural-sort [ length ] keep
concat [ prime? ] filter members natural-sort [ length ] keep
8 group simple-table. "\nFound %d concatenated primes.\n" printf</lang>
8 group simple-table. "\nFound %d concatenated primes.\n" printf</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 481: Line 481:
=={{header|Go}}==
=={{header|Go}}==
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 521: Line 521:
}
}
fmt.Println("\n\nFound", len(results), "such concatenated primes.")
fmt.Println("\n\nFound", len(results), "such concatenated primes.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 544: Line 544:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Applicative
<syntaxhighlight lang="haskell">import Control.Applicative
import Data.List ( sort )
import Data.List ( sort )
import Data.List.Split ( chunksOf )
import Data.List.Split ( chunksOf )
Line 565: Line 565:
main :: IO ( )
main :: IO ( )
main = do
main = do
mapM_ print $ chunksOf 15 solution</lang>
mapM_ print $ chunksOf 15 solution</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 584: Line 584:


'''Preliminaries'''
'''Preliminaries'''
<lang jq>def is_prime:
<syntaxhighlight lang="jq">def is_prime:
. as $n
. as $n
| if ($n < 2) then false
| if ($n < 2) then false
Line 613: Line 613:


def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
</syntaxhighlight>
</lang>
'''The task'''
'''The task'''
<lang jq># Emit [p1,p2] where p1 < p2 < . and the concatenation is prime
<syntaxhighlight lang="jq"># Emit [p1,p2] where p1 < p2 < . and the concatenation is prime
def concatenative_primes:
def concatenative_primes:
primes
primes
Line 624: Line 624:


[100 | concatenative_primes | join("||")]
[100 | concatenative_primes | join("||")]
| (nwise(10) | map(lpad(6)) | join(" "))</lang>
| (nwise(10) | map(lpad(6)) | join(" "))</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 642: Line 642:
</pre>
</pre>
=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function catprimes()
function catprimes()
Line 655: Line 655:
foreach(p -> print(lpad(last(p), 5), first(p) % 16 == 0 ? "\n" : ""),
foreach(p -> print(lpad(last(p), 5), first(p) % 16 == 0 ? "\n" : ""),
catprimes() |> enumerate)
catprimes() |> enumerate)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
23 37 53 73 113 137 173 193 197 211 223 229 233 241 271 283
23 37 53 73 113 137 173 193 197 211 223 229 233 241 271 283
Line 668: Line 668:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Select[Catenate /* FromDigits /@ Map[IntegerDigits, Tuples[Prime[Range[PrimePi[100]]], 2], {2}], PrimeQ] // Union</lang>
<syntaxhighlight lang="mathematica">Select[Catenate /* FromDigits /@ Map[IntegerDigits, Tuples[Prime[Range[PrimePi[100]]], 2], {2}], PrimeQ] // Union</syntaxhighlight>
{{out}}
{{out}}
<pre>{23, 37, 53, 73, 113, 137, 173, 193, 197, 211, 223, 229, 233, 241, 271, 283, 293, 311, 313, 317, 331, 337, 347, 353, 359, 367, 373, 379, 383, 389, 397, 433, 523, 541, 547, 571, 593, 613, 617, 673, 677, 719, 733, 743, 761, 773, 797, 977, 1117, 1123, 1129, 1153, 1171, 1319, 1361, 1367, 1373, 1723, 1741, 1747, 1753, 1759, 1783, 1789, 1913, 1931, 1973, 1979, 1997, 2311, 2341, 2347, 2371, 2383, 2389, 2917, 2953, 2971, 3119, 3137, 3167, 3719, 3761, 3767, 3779, 3797, 4111, 4129, 4153, 4159, 4337, 4373, 4397, 4723, 4729, 4759, 4783, 4789, 5323, 5347, 5923, 5953, 6113, 6131, 6143, 6173, 6197, 6719, 6737, 6761, 6779, 7129, 7159, 7331, 7919, 7937, 8311, 8317, 8329, 8353, 8389, 8923, 8929, 8941, 8971, 9719, 9743, 9767}</pre>
<pre>{23, 37, 53, 73, 113, 137, 173, 193, 197, 211, 223, 229, 233, 241, 271, 283, 293, 311, 313, 317, 331, 337, 347, 353, 359, 367, 373, 379, 383, 389, 397, 433, 523, 541, 547, 571, 593, 613, 617, 673, 677, 719, 733, 743, 761, 773, 797, 977, 1117, 1123, 1129, 1153, 1171, 1319, 1361, 1367, 1373, 1723, 1741, 1747, 1753, 1759, 1783, 1789, 1913, 1931, 1973, 1979, 1997, 2311, 2341, 2347, 2371, 2383, 2389, 2917, 2953, 2971, 3119, 3137, 3167, 3719, 3761, 3767, 3779, 3797, 4111, 4129, 4153, 4159, 4337, 4373, 4397, 4723, 4729, 4759, 4783, 4789, 5323, 5347, 5923, 5953, 6113, 6131, 6143, 6173, 6197, 6719, 6737, 6761, 6779, 7129, 7159, 7331, 7919, 7937, 8311, 8317, 8329, 8353, 8389, 8923, 8929, 8941, 8971, 9719, 9743, 9767}</pre>


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strutils, sugar
<syntaxhighlight lang="nim">import strutils, sugar


func isPrime(n: Positive): bool =
func isPrime(n: Positive): bool =
Line 700: Line 700:
for n in concatPrimes:
for n in concatPrimes:
stdout.write ($n).align(4), if i mod 16 == 0: '\n' else: ' '
stdout.write ($n).align(4), if i mod 16 == 0: '\n' else: ' '
inc i</lang>
inc i</syntaxhighlight>


{{out}}
{{out}}
Line 714: Line 714:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Concatenate_two_primes_is_also_prime
use strict; # https://rosettacode.org/wiki/Concatenate_two_primes_is_also_prime
Line 724: Line 724:
my @valid = uniq sort { $a <=> $b } grep is_prime($_),
my @valid = uniq sort { $a <=> $b } grep is_prime($_),
map { my $prefix = $_; map "$prefix$_", @primes } @primes;
map { my $prefix = $_; map "$prefix$_", @primes } @primes;
print @valid . " primes found\n\n@valid\n" =~ s/.{79}\K /\n/gr;</lang>
print @valid . " primes found\n\n@valid\n" =~ s/.{79}\K /\n/gr;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 740: Line 740:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span>
Line 754: Line 754:
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- (see note)</span>
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- (see note)</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 such primes: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</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 such primes: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
<small>Note: The deep_copy() [for pwa/p2js] on such calls to unique() would be unnecessary were result routine-local, due to automatic pbr, but here that variable is file-level.<br>
<small>Note: The deep_copy() [for pwa/p2js] on such calls to unique() would be unnecessary were result routine-local, due to automatic pbr, but here that variable is file-level.<br>
While the (human-readable) error message (without it) is deep within sort(), the call stack makes it clear where that call should best go.</small>
While the (human-readable) error message (without it) is deep within sort(), the call stack makes it clear where that call should best go.</small>
Line 764: Line 764:
=={{header|Raku}}==
=={{header|Raku}}==
Inefficient, but for a limit of 100, who cares?
Inefficient, but for a limit of 100, who cares?
<lang perl6>my @p = ^1e2 .grep: *.is-prime;
<syntaxhighlight lang="raku" line>my @p = ^1e2 .grep: *.is-prime;


say display ( @p X~ @p ).grep( *.is-prime ).unique.sort( +* );
say display ( @p X~ @p ).grep( *.is-prime ).unique.sort( +* );
Line 771: Line 771:
cache $list;
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>128 matching:
<pre>128 matching:
Line 789: Line 789:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds base ten neighbor primes P1 & P2, when concatenated, is also a prime.*/
<syntaxhighlight lang="rexx">/*REXX pgm finds base ten neighbor primes P1 & P2, when concatenated, is also a prime.*/
parse arg hip cols . /*obtain optional arguments from the CL*/
parse arg hip cols . /*obtain optional arguments from the CL*/
if hip=='' | hip=="," then hip= 100 /*Not specified? Then use the default.*/
if hip=='' | hip=="," then hip= 100 /*Not specified? Then use the default.*/
Line 836: Line 836:
#= #+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# */
if j<hip then ##= # /*find a shortcut for the 1st DO loop. */
if j<hip then ##= # /*find a shortcut for the 1st DO loop. */
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 860: Line 860:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"
see "working..." + nl
see "working..." + nl
Line 903: Line 903:
see nl + "Found " + row + " prime numbers" + nl
see nl + "Found " + row + " prime numbers" + nl
see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 926: Line 926:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>require "prime"
<syntaxhighlight lang="ruby">require "prime"


concats = Prime.each(100).to_a.repeated_permutation(2).filter_map do |pair|
concats = Prime.each(100).to_a.repeated_permutation(2).filter_map do |pair|
Line 934: Line 934:
concats = concats.sort.uniq
concats = concats.sort.uniq


concats.each_slice(10){|slice|puts slice.map{|el| el.to_s.ljust(6)}.join }</lang>
concats.each_slice(10){|slice|puts slice.map{|el| el.to_s.ljust(6)}.join }</syntaxhighlight>
{{out}}
{{out}}
<pre>23 37 53 73 113 137 173 193 197 211
<pre>23 37 53 73 113 137 173 193 197 211
Line 951: Line 951:
</pre>
</pre>
=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var upto = 100
<syntaxhighlight lang="ruby">var upto = 100
var arr = upto.primes
var arr = upto.primes
var base = 10
var base = 10
Line 966: Line 966:
})
})


say "\nFound #{arr.len} such concatenated primes."</lang>
say "\nFound #{arr.len} such concatenated primes."</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 991: Line 991:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
<lang ecmascript>import "/math" for Int
<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,008: Line 1,008:
System.print("Two primes under 100 concatenated together to form another prime:")
System.print("Two primes under 100 concatenated together to form another prime:")
for (chunk in Lst.chunks(results, 10)) Fmt.print("$,6d", chunk)
for (chunk in Lst.chunks(results, 10)) Fmt.print("$,6d", chunk)
System.print("\nFound %(results.count) such concatenated primes.")</lang>
System.print("\nFound %(results.count) such concatenated primes.")</syntaxhighlight>


{{out}}
{{out}}
Line 1,031: Line 1,031:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number
<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,062: Line 1,062:
Text(0, " such concatenated primes found.
Text(0, " such concatenated primes found.
");
");
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}