Concatenate two primes is also prime: Difference between revisions
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a Raku example) |
|||
Line 4: | Line 4: | ||
<br><br> |
<br><br> |
||
=={{header|Perl}}== |
|||
<lang perl>#!/usr/bin/perl |
|||
use strict; # https://rosettacode.org/wiki/Concatenate_two_primes_is_also_prime |
|||
use warnings; |
|||
use ntheory qw( primes is_prime ); |
|||
use List::Util qw( uniq ); |
|||
my @primes = @{ primes(100) }; |
|||
my @valid = uniq sort { $a <=> $b } grep is_prime($_), |
|||
map { my $prefix = $_; map "$prefix$_", @primes } @primes; |
|||
print @valid . " primes found\n\n@valid\n" =~ s/.{79}\K /\n/gr;</lang> |
|||
{{out}} |
|||
<pre> |
|||
128 primes found |
|||
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|Raku}}== |
=={{header|Raku}}== |
Revision as of 18:55, 28 July 2021
- Task
- Concatenate two primes (p1,p2) is also prime, where p1,p2 < 100
Perl
<lang perl>#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Concatenate_two_primes_is_also_prime use warnings; use ntheory qw( primes is_prime ); use List::Util qw( uniq );
my @primes = @{ primes(100) }; my @valid = uniq sort { $a <=> $b } grep is_prime($_),
map { my $prefix = $_; map "$prefix$_", @primes } @primes;
print @valid . " primes found\n\n@valid\n" =~ s/.{79}\K /\n/gr;</lang>
- Output:
128 primes found 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
Raku
Inefficient, but for a limit of 100, who cares? <lang perl6>my @p = ^1e2 .grep: *.is-prime;
say display ( @p X~ @p ).grep( *.is-prime ).unique.sort( +* );
sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n") {
cache $list; $title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</lang>
- Output:
128 matching: 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
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Concatenate two primes is also prime:" + nl row = 0 prime = []
for n = 1 to 100
for m = 1 to 100 ps1 = string(n) ps2 = string(m) ps12 = ps1 + ps2 ps21 = ps2 + ps1 p1 = number(ps12) p2 = number(ps21) if isprime(n) and isprime(m) if isprime(p1) pf = find(prime,p1) if pf < 1 add(prime,p1) ok ok if isprime(p2) pf = find(prime,p2) if pf < 1 add(prime,p2) ok ok ok next
next
prime = sort(prime) for n = 1 to len(prime)
row++ see "" + prime[n] + " " if row%10 = 0 see nl ok
next
see nl + "Found " + row + " prime numbers" + nl see "done..." + nl </lang>
- Output:
working... Concatenate two primes is also prime: 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 Found 128 prime numbers done...
Wren
<lang ecmascript>import "/math" for Int import "/fmt" for Fmt import "/seq" for Lst
var limit = 99 var primes = Int.primeSieve(limit) var results = [] for (p in primes) {
for (q in primes) { var pq = (q < 10) ? p*10 + q : p*100 + q if (Int.isPrime(pq)) results.add(pq) }
} results = Lst.distinct(results) results.sort() System.print("Two primes under 100 concatenated together to form another prime:") for (chunk in Lst.chunks(results, 10)) Fmt.print("$,6d", chunk) System.print("\nFound %(results.count) such concatenated primes.")</lang>
- Output:
Two primes under 100 concatenated together to form another prime: 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 1,117 1,123 1,129 1,153 1,171 1,319 1,361 1,367 1,373 1,723 1,741 1,747 1,753 1,759 1,783 1,789 1,913 1,931 1,973 1,979 1,997 2,311 2,341 2,347 2,371 2,383 2,389 2,917 2,953 2,971 3,119 3,137 3,167 3,719 3,761 3,767 3,779 3,797 4,111 4,129 4,153 4,159 4,337 4,373 4,397 4,723 4,729 4,759 4,783 4,789 5,323 5,347 5,923 5,953 6,113 6,131 6,143 6,173 6,197 6,719 6,737 6,761 6,779 7,129 7,159 7,331 7,919 7,937 8,311 8,317 8,329 8,353 8,389 8,923 8,929 8,941 8,971 9,719 9,743 9,767 Found 128 such concatenated primes.