Primes whose first and last number is 3: Difference between revisions

m
syntax highlighting fixup automation
(Added 11l)
m (syntax highlighting fixup automation)
Line 14:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F is_prime(n)
I n == 2
R 1B
Line 42:
print(‘#4’.format(n), end' I (L.index + 1) % 11 == 0 {"\n"} E ‘ ’)
 
print("\nFound "count‘ primes starting and ending with 3 below 1000000.’)</langsyntaxhighlight>
 
{{out}}
Line 56:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE Func IsSpecialPrime(INT i BYTE ARRAY primes)
Line 96:
OD
PrintF("%E%EThere are %I primes",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Primes_whose_first_and_last_number_is_3.png Screenshot from Atari 8-bit computer]
Line 109:
{{Trans|ALGOL W}} With added stretch goal. As with the Go and other samples, generates the candidate sequence.
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find some primes whose first and last digits are 3 #
INT max prime = 1 000 000; # largest number to consider #
# sieve the primes to max prime #
Line 129:
FOR i FROM 0 BY 10 TO 99 990 DO IF prime[ 300 003 + i ] THEN p3count +:= 1 FI OD;
print( ( newline, "Found ", whole( p3count, 0 ), " ""3x3"" primes below 1000000", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 140:
=={{header|ALGOL W}}==
As with the Go and oher samples, finds the numbers by generating the candidate seuence.
<langsyntaxhighlight lang="algolw">begin % find some primes whose first and last digits are 3 %
integer MAX_PRIME;
MAX_PRIME := 4000;
Line 166:
for i := 0 step 10 until 990 do p( 3003 + i );
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 174:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIMES_WHOSE_FIRST_AND_LAST_NUMBER_IS_3.AWK
BEGIN {
Line 206:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 219:
=={{header|C}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
#include<math.h>
Line 250:
printf( "\n\nThere were %d primes of the form 3x3 below one million.\n", np );
return 0;
}</langsyntaxhighlight>
{{out}}<pre>
3 313 353 373 383 3023 3083 3163 3203 3253
Line 261:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
//3 Sandwich Primes. Nigel Galloway: July 25th., 2021
primes32()|>Seq.takeWhile((>)4000)|>Seq.filter(fun n->n%10=3 && (n=3||(n>29 && n<40)||(n>299 && n<400)||n>2999))|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 272:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting grouping io kernel lists lists.lazy math
math.functions math.primes sequences ;
 
Line 293:
 
3 1,000,000 surrounded llength
"Found %d primes beginning and ending with 3 under 1,000,000.\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 326:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">np := 1;
!(3,' ');
for d = 1 to 5 do
Line 343:
!!;
!!('There were ',np,' primes of the form 3...3 below 1,000,000');
</syntaxhighlight>
</lang>
{{out}}<pre>
3 313 353 373 383 3023 3083 3163 3203 3253
Line 357:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
dim as integer np = 1, d, n, i
Line 374:
next d
print : print
print "There were ";np;" 3...3 primes below 1000000"</langsyntaxhighlight>
{{out}}<pre>
3 313 353 373 383 3023 3083 3163 3203 3253
Line 386:
=={{header|Go}}==
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 428:
pcc := rcu.Commatize(pc)
fmt.Println("\nFound", pcc, "primes under 1,000,000 which begin and end with 3.")
}</langsyntaxhighlight>
 
{{out}}
Line 447:
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<langsyntaxhighlight lang="jq"># For the stretch goal:
def count(s): reduce s as $x (null; .+1);
 
Line 460:
 
task(2),
"\nStretch goal: \(count( task(4) ))"</langsyntaxhighlight>
{{out}}
<pre>
Line 501:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
isxbyx(n, base=10, dig=3) = n ÷ prevpow(base, n) == dig && n % base == dig
Line 512:
println("\nTotal $(d)x$d primes less than 1,000,000: ", length(p3x3(1_000_000, 10, d)), ".")
end
</langsyntaxhighlight>{{out}}
<pre>
Line 547:
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Cases[NestWhileList[NextPrime,
2, # < 4000 &], _?(IntegerDigits[#][[{1, -1}]] === {3, 3} &)]</langsyntaxhighlight>
 
{{out}}<pre>
Line 555:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat
 
func isPrime(n: Positive): bool =
Line 584:
stdout.write &"{n:4}", if (i + 1) mod 11 == 0: '\n' else: ' '
 
echo &"\nFound {count} primes starting and ending with 3 below 1_000_000."</langsyntaxhighlight>
 
{{out}}
Line 596:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Primes_whose_first_and_last_number_is_3
Line 605:
my $n33 = grep /^3/ && /3$/, @{ primes( 1_000_000 ) };
print @n33 . " under 4000\n\n@n33" =~ s/.{75}\K /\n/gr,
"\n\n$n33 under 1000000\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 618:
=={{header|Phix}}==
Works for any digit, partly inspired by the Wren entry.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">primes_with_first_and_last_digit</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p10</span><span style="color: #0000FF;">)</span>
Line 643:
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes_with_first_and_last_digit</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</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;">"There are %d primes under 1,000,000 which begin and end in 3\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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 656:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my $upto = 1e4;
 
for 1,3,5,7,9 -> $bracket {
Line 666:
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</langsyntaxhighlight>
{{out}}
<pre>Primes up to 10000 bracketed by 1 - 39 matching:
Line 699:
 
Also, &nbsp; if a negative &nbsp; '''cols''' &nbsp; is specified, &nbsp; only the &nbsp; ''count'' &nbsp; of primes found is shown.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds and displays primes (base ten) that contain a leading and trailing 3. */
parse arg hi cols dig . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 4000 /*Not specified? Then use the default.*/
Line 743:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j /*bump # of Ps; assign next P; P square*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 762:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 781:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 795:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func numbers_with_edges(upto, base = 10, s = [3]) {
Enumerator({|callback|
callback(s.digits2num(base))
Line 822:
var count = numbers_with_edges(n).grep{.is_prime}.len
say "\nThere are #{count} primes <= #{n.commify} which begin and end in 3"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 841:
 
===Basic task===
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/trait" for Stepped
import "/seq" for Lst
Line 852:
System.print("Primes under 4,000 which begin and end in 3:")
for (chunk in Lst.chunks(primes, 11)) Fmt.print("$,5d", chunk)
System.print("\nFound %(primes.count) such primes.")</langsyntaxhighlight>
 
{{out}}
Line 866:
===More general===
This version deals with primes (in base 10) beginning and ending with any specified digit and with up to a given number of digits.
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/trait" for Stepped
import "/seq" for Lst
Line 899:
var primes = getQualifyingPrimes.call(x, d)
Fmt.print("Found $,d primes under $,d which begin and end with $d.\n", primes.count, 10.pow(d), x)
}</langsyntaxhighlight>
 
{{out}}
Line 954:
 
=={{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 991:
Text(0, " such primes found below 1,000,000.
");
]</langsyntaxhighlight>
 
{{out}}
10,333

edits