Primes whose first and last number is 3: Difference between revisions
Primes whose first and last number is 3 (view source)
Revision as of 11:17, 28 August 2022
, 1 year agosyntax highlighting fixup automation
Alextretyak (talk | contribs) (Added 11l) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 14:
{{trans|Nim}}
<
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.’)</
{{out}}
Line 56:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
BYTE Func IsSpecialPrime(INT i BYTE ARRAY primes)
Line 96:
OD
PrintF("%E%EThere are %I primes",count)
RETURN</
{{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}}
<
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</
{{out}}
<pre>
Line 140:
=={{header|ALGOL W}}==
As with the Go and oher samples, finds the numbers by generating the candidate seuence.
<
integer MAX_PRIME;
MAX_PRIME := 4000;
Line 166:
for i := 0 step 10 until 990 do p( 3003 + i );
end
end.</
{{out}}
<pre>
Line 174:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PRIMES_WHOSE_FIRST_AND_LAST_NUMBER_IS_3.AWK
BEGIN {
Line 206:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 219:
=={{header|C}}==
{{trans|FreeBASIC}}
<
#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;
}</
{{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#)]
<
//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>
{{out}}
<pre>
Line 272:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
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</
{{out}}
<pre>
Line 326:
=={{header|Fermat}}==
<
!(3,' ');
for d = 1 to 5 do
Line 343:
!!;
!!('There were ',np,' primes of the form 3...3 below 1,000,000');
</syntaxhighlight>
{{out}}<pre>
3 313 353 373 383 3023 3083 3163 3203 3253
Line 357:
=={{header|FreeBASIC}}==
<
dim as integer np = 1, d, n, i
Line 374:
next d
print : print
print "There were ";np;" 3...3 primes below 1000000"</
{{out}}<pre>
3 313 353 373 383 3023 3083 3163 3203 3253
Line 386:
=={{header|Go}}==
{{libheader|Go-rcu}}
<
import (
Line 428:
pcc := rcu.Commatize(pc)
fmt.Println("\nFound", pcc, "primes under 1,000,000 which begin and end with 3.")
}</
{{out}}
Line 447:
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<
def count(s): reduce s as $x (null; .+1);
Line 460:
task(2),
"\nStretch goal: \(count( task(4) ))"</
{{out}}
<pre>
Line 501:
=={{header|Julia}}==
<
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
</
<pre>
Line 547:
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
2, # < 4000 &], _?(IntegerDigits[#][[{1, -1}]] === {3, 3} &)]</
{{out}}<pre>
Line 555:
=={{header|Nim}}==
<
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."</
{{out}}
Line 596:
=={{header|Perl}}==
{{libheader|ntheory}}
<
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";</
{{out}}
<pre>
Line 618:
=={{header|Phix}}==
Works for any digit, partly inspired by the Wren entry.
<!--<
<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>
<!--</
{{out}}
<pre>
Line 656:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
for 1,3,5,7,9 -> $bracket {
Line 666:
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</
{{out}}
<pre>Primes up to 10000 bracketed by 1 - 39 matching:
Line 699:
Also, if a negative '''cols''' is specified, only the ''count'' of primes found is shown.
<
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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 762:
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 781:
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 795:
=={{header|Sidef}}==
<
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"
}</
{{out}}
<pre>
Line 841:
===Basic task===
<
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.")</
{{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.
<
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)
}</
{{out}}
Line 954:
=={{header|XPL0}}==
<
int N, I;
[if N <= 1 then return false;
Line 991:
Text(0, " such primes found below 1,000,000.
");
]</
{{out}}
|