Sexy primes: Difference between revisions
Content added Content deleted
(Sexy primes in FreeBASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 32: | Line 32: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V LIMIT = 1'000'000 |
||
F get_primes(limit) |
F get_primes(limit) |
||
V is_prime = [0B] * 2 [+] [1B] * (limit - 1) |
V is_prime = [0B] * 2 [+] [1B] * (limit - 1) |
||
Line 72: | Line 72: | ||
print("\nThere are #. unsexy primes ending with ...".format(unsexy.len)) |
print("\nThere are #. unsexy primes ending with ...".format(unsexy.len)) |
||
L(usx) unsexy[(len)-10..] |
L(usx) unsexy[(len)-10..] |
||
print(‘ ’usx)</ |
print(‘ ’usx)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 112: | Line 112: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% find some sexy primes - primes that differ from another prime by 6 % |
% find some sexy primes - primes that differ from another prime by 6 % |
||
% implements the sieve of Eratosthenes % |
% implements the sieve of Eratosthenes % |
||
Line 251: | Line 251: | ||
showPrimes( 1, unsexy, 10, "unsexy primes", MAX_SEXY ) |
showPrimes( 1, unsexy, 10, "unsexy primes", MAX_SEXY ) |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 267: | Line 267: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SEXY_PRIMES.AWK |
# syntax: GAWK -f SEXY_PRIMES.AWK |
||
BEGIN { |
BEGIN { |
||
Line 323: | Line 323: | ||
s[key] = str |
s[key] = str |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 346: | Line 346: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Similar approach to the Go entry but only stores the arrays that need to be printed out. |
Similar approach to the Go entry but only stores the arrays that need to be printed out. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 516: | Line 516: | ||
free(sv); |
free(sv); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 543: | Line 543: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|Boost}} |
{{libheader|Boost}} |
||
< |
<syntaxhighlight lang="cpp">#include <array> |
||
#include <iostream> |
#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 613: | Line 613: | ||
cout << '\n'; |
cout << '\n'; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Contents of prime_sieve.hpp: |
Contents of prime_sieve.hpp: |
||
< |
<syntaxhighlight lang="cpp">#ifndef PRIME_SIEVE_HPP |
||
#define PRIME_SIEVE_HPP |
#define PRIME_SIEVE_HPP |
||
Line 667: | Line 667: | ||
} |
} |
||
#endif</ |
#endif</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 692: | Line 692: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Sexy primes. Nigel Galloway: October 2nd., 2018 |
// Sexy primes. Nigel Galloway: October 2nd., 2018 |
||
let n=pCache |> Seq.takeWhile(fun n->n<1000035) |> Seq.filter(fun n->(not (isPrime(n+6)) && (not isPrime(n-6))))) |> Array.ofSeq |
let n=pCache |> Seq.takeWhile(fun n->n<1000035) |> Seq.filter(fun n->(not (isPrime(n+6)) && (not isPrime(n-6))))) |> Array.ofSeq |
||
Line 709: | Line 709: | ||
printfn "There are %d sexy prime quintuplets all components of which are less than 1,000,035. The last 5 are:" nigel.Length |
printfn "There are %d sexy prime quintuplets all components of which are less than 1,000,035. The last 5 are:" nigel.Length |
||
Array.skip (nigel.Length-5) nigel |> Array.iter(fun n->printf "(%d,%d,%d,%d,%d) " (n-24) (n-18) (n-12) (n-6) n); printfn "" |
Array.skip (nigel.Length-5) nigel |> Array.iter(fun n->printf "(%d,%d,%d,%d,%d) " (n-24) (n-18) (n-12) (n-6) n); printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 725: | Line 725: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: combinators.short-circuit fry interpolate io kernel |
||
literals locals make math math.primes math.ranges prettyprint qw |
literals locals make math math.primes math.ranges prettyprint qw |
||
sequences tools.memory.private ; |
sequences tools.memory.private ; |
||
Line 768: | Line 768: | ||
: main ( -- ) 2 5 [a,b] [ show-tuplets ] each show-unsexy ; |
: main ( -- ) 2 5 [a,b] [ show-tuplets ] each show-unsexy ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 789: | Line 789: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Uinteger) As Boolean |
||
If ValorEval < 2 Then Return False |
If ValorEval < 2 Then Return False |
||
If ValorEval Mod 2 = 0 Then Return ValorEval = 2 |
If ValorEval Mod 2 = 0 Then Return ValorEval = 2 |
||
Line 859: | Line 859: | ||
Next I |
Next I |
||
Print Chr(8); " " |
Print Chr(8); " " |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 78500 primes less than 1000035 |
<pre> 78500 primes less than 1000035 |
||
Line 892: | Line 892: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,004: | Line 1,004: | ||
le, n, verb = printHelper("unsexy primes", len(unsexy), lim, 10) |
le, n, verb = printHelper("unsexy primes", len(unsexy), lim, 10) |
||
fmt.Printf("The last %d %s:\n %v\n\n", n, verb, unsexy[le-n:]) |
fmt.Printf("The last %d %s:\n %v\n\n", n, verb, unsexy[le-n:]) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,030: | Line 1,030: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Uses Library primes. https://hackage.haskell.org/package/primes (wheel sieve). |
Uses Library primes. https://hackage.haskell.org/package/primes (wheel sieve). |
||
< |
<syntaxhighlight lang="haskell">import Text.Printf (printf) |
||
import Data.Numbers.Primes (isPrime, primes) |
import Data.Numbers.Primes (isPrime, primes) |
||
Line 1,066: | Line 1,066: | ||
where (pairs, triplets, quads, quins, unsexy) = foldr groups ([], [], [], [], []) $ takeWhile (< 1000035) primes |
where (pairs, triplets, quads, quins, unsexy) = foldr groups ([], [], [], [], []) $ takeWhile (< 1000035) primes |
||
lastFive xs = show $ drop (length xs - 5) xs |
lastFive xs = show $ drop (length xs - 5) xs |
||
lastFiveText = " Last 5 : %s\n\n"</ |
lastFiveText = " Last 5 : %s\n\n"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,085: | Line 1,085: | ||
</pre> |
</pre> |
||
Slight variation which only holds on to the display results. Does not perform any better than above though. Both run ~ 250ms. |
Slight variation which only holds on to the display results. Does not perform any better than above though. Both run ~ 250ms. |
||
< |
<syntaxhighlight lang="haskell">{-# LANGUAGE TemplateHaskell #-} |
||
import Control.Lens (makeLenses, over, (^.), to, view) |
import Control.Lens (makeLenses, over, (^.), to, view) |
||
import Data.Numbers.Primes (isPrime, primes) |
import Data.Numbers.Primes (isPrime, primes) |
||
Line 1,134: | Line 1,134: | ||
collectGroups = foldl collect initialGroups . takeWhile (< 1000035) |
collectGroups = foldl collect initialGroups . takeWhile (< 1000035) |
||
display :: Show a => [a] -> String |
display :: Show a => [a] -> String |
||
display = show . reverse</ |
display = show . reverse</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">NB. Primes Not Greater Than (the input) |
||
NB. The 1 _1 p: ... logic here allows the input value to |
NB. The 1 _1 p: ... logic here allows the input value to |
||
NB. be included in the list in the case it itself is prime |
NB. be included in the list in the case it itself is prime |
||
Line 1,163: | Line 1,163: | ||
us =. p (] #~ 1 +:/@:p: +/)~ (+,-) pd NB. Unsexy numbers |
us =. p (] #~ 1 +:/@:p: +/)~ (+,-) pd NB. Unsexy numbers |
||
( (# ; _10&{.) us ) , (#&.> g) ,. l |
( (# ; _10&{.) us ) , (#&.> g) ,. l |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="j"> r =: 6 5 sp 1000035 NB. 6=sex=prime distance, 5=max orgy size |
||
(;:'Group Count Examples') , (;:'Unsexy Pairs Triplets Quadruplets Quintuplets') ,. r |
(;:'Group Count Examples') , (;:'Unsexy Pairs Triplets Quadruplets Quintuplets') ,. r |
||
+-----------+-----+----------------------------------------------------------------------+ |
+-----------+-----+----------------------------------------------------------------------+ |
||
Line 1,192: | Line 1,192: | ||
+-----------+-----+----------------------------------------------------------------------+ |
+-----------+-----+----------------------------------------------------------------------+ |
||
|Quintuplets|1 |5 11 17 23 29 | |
|Quintuplets|1 |5 11 17 23 29 | |
||
+-----------+-----+----------------------------------------------------------------------+</ |
+-----------+-----+----------------------------------------------------------------------+</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java"> |
||
import java.util.ArrayList; |
import java.util.ArrayList; |
||
import java.util.List; |
import java.util.List; |
||
Line 1,269: | Line 1,269: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,291: | Line 1,291: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"> |
||
using Primes |
using Primes |
||
Line 1,367: | Line 1,367: | ||
end |
end |
||
primesbysexiness(1000035) </ |
primesbysexiness(1000035) </syntaxhighlight> {{output}} <pre> |
||
There are: |
There are: |
||
16386 twins, |
16386 twins, |
||
Line 1,383: | Line 1,383: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">// Version 1.2.71 |
||
fun sieve(lim: Int): BooleanArray { |
fun sieve(lim: Int): BooleanArray { |
||
Line 1,462: | Line 1,462: | ||
var (nu, verbu) = printHelper("unsexy primes", unsexy.size, lim, 10) |
var (nu, verbu) = printHelper("unsexy primes", unsexy.size, lim, 10) |
||
System.out.printf("The last %d %s:\n %s\n\n", nu, verbu, unsexy.takeLast(nu)) |
System.out.printf("The last %d %s:\n %s\n\n", nu, verbu, unsexy.takeLast(nu)) |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 1,489: | Line 1,489: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">local N = 1000035 |
||
-- FUNCS: |
-- FUNCS: |
||
Line 1,512: | Line 1,512: | ||
end |
end |
||
local unsexy = primes:filter(function(v) return not (v>=N or sieve[v-6] or sieve[v+6]) end) |
local unsexy = primes:filter(function(v) return not (v>=N or sieve[v-6] or sieve[v+6]) end) |
||
print(#unsexy .. " unsexy, ending with: " ..unsexy:lastn(10):concat(" "))</ |
print(#unsexy .. " unsexy, ending with: " ..unsexy:lastn(10):concat(" "))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>16386 pairs, ending with: (999371 999377) (999431 999437) (999721 999727) (999763 999769) (999953 999959) |
<pre>16386 pairs, ending with: (999371 999377) (999431 999437) (999721 999727) (999763 999769) (999953 999959) |
||
Line 1,521: | Line 1,521: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[AllSublengths] |
||
AllSublengths[l_List] := If[Length[l] > 2, |
AllSublengths[l_List] := If[Length[l] > 2, |
||
Catenate[Partition[l, #, 1] & /@ Range[2, Length[l]]] |
Catenate[Partition[l, #, 1] & /@ Range[2, Length[l]]] |
||
Line 1,551: | Line 1,551: | ||
sel // Column |
sel // Column |
||
Select[Complement[primes, DeleteDuplicates[Catenate@sp]][[-20 ;;]], ! (PrimeQ[# + 6] \[Or] PrimeQ[# - 6]) &][[-10 ;;]] // Column</ |
Select[Complement[primes, DeleteDuplicates[Catenate@sp]][[-20 ;;]], ! (PrimeQ[# + 6] \[Or] PrimeQ[# - 6]) &][[-10 ;;]] // Column</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>16386 |
<pre>16386 |
||
Line 1,588: | Line 1,588: | ||
This Nim version uses Kotlin algorithm with several differences. In particular, we have chosen to store only the first term of groups as others can be retrieved by computation. But it complicates somewhat the printing of results. |
This Nim version uses Kotlin algorithm with several differences. In particular, we have chosen to store only the first term of groups as others can be retrieved by computation. But it complicates somewhat the printing of results. |
||
< |
<syntaxhighlight lang="nim">import math, strformat, strutils |
||
const Lim = 1_000_035 |
const Lim = 1_000_035 |
||
Line 1,667: | Line 1,667: | ||
printResult(Quadruplets, quads, 5) |
printResult(Quadruplets, quads, 5) |
||
printResult(Quintuplets, quints, 5) |
printResult(Quintuplets, quints, 5) |
||
printResult(Unsexy, unsexy, 10)</ |
printResult(Unsexy, unsexy, 10)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,698: | Line 1,698: | ||
37907606 unsexy primes // = 50847538-2*6849047+758163-1 |
37907606 unsexy primes // = 50847538-2*6849047+758163-1 |
||
It seems so, not a proove. |
It seems so, not a proove. |
||
< |
<syntaxhighlight lang="pascal">program SexyPrimes; |
||
uses |
uses |
||
Line 1,877: | Line 1,877: | ||
writeln(unsexyprimes,' unsexy primes'); |
writeln(unsexyprimes,' unsexy primes'); |
||
OutLastUnsexy(10); |
OutLastUnsexy(10); |
||
end.</ |
end.</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 1,920: | Line 1,920: | ||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
We will use the prime iterator and primality test from the <code>ntheory</code> module. |
We will use the prime iterator and primality test from the <code>ntheory</code> module. |
||
< |
<syntaxhighlight lang="perl">use ntheory qw/prime_iterator is_prime/; |
||
sub tuple_tail { |
sub tuple_tail { |
||
Line 1,966: | Line 1,966: | ||
print "Number of unsexy primes less than $cmax: ". comma(scalar @{$primes{unsexy}}) . "\n"; |
print "Number of unsexy primes less than $cmax: ". comma(scalar @{$primes{unsexy}}) . "\n"; |
||
print " Last 10 unsexy primes less than $cmax: ". join(' ', @{$primes{unsexy}}[-10..-1]) . "\n";</ |
print " Last 10 unsexy primes less than $cmax: ". join(' ', @{$primes{unsexy}}[-10..-1]) . "\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Total primes less than 1,000,035: 78,500 |
<pre>Total primes less than 1,000,035: 78,500 |
||
Line 1,990: | Line 1,990: | ||
The cluster sieve becomes more efficient as the number of terms increases. See for example [[oeis:a213646|OEIS Prime 11-tuplets]]. |
The cluster sieve becomes more efficient as the number of terms increases. See for example [[oeis:a213646|OEIS Prime 11-tuplets]]. |
||
< |
<syntaxhighlight lang="perl">use ntheory qw/sieve_prime_cluster forprimes is_prime/; |
||
# ... identical helper functions |
# ... identical helper functions |
||
Line 2,007: | Line 2,007: | ||
} $max-1; |
} $max-1; |
||
# ... identical output code</ |
# ... identical output code</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">create_sieve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">create_sieve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sieve</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sieve</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span> |
||
Line 2,082: | Line 2,082: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">results</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: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">results</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,095: | Line 2,095: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
< |
<syntaxhighlight lang="prolog">sexy_prime_group(1, N, _, [N]):- |
||
is_prime(N), |
is_prime(N), |
||
!. |
!. |
||
Line 2,141: | Line 2,141: | ||
main:- |
main:- |
||
main(1000035).</ |
main(1000035).</syntaxhighlight> |
||
Module for finding prime numbers up to some limit: |
Module for finding prime numbers up to some limit: |
||
< |
<syntaxhighlight lang="prolog">:- module(prime_numbers, [find_prime_numbers/1, is_prime/1]). |
||
:- dynamic is_prime/1. |
:- dynamic is_prime/1. |
||
Line 2,185: | Line 2,185: | ||
cross_out(S, N, P):- |
cross_out(S, N, P):- |
||
Q is S + 2 * P, |
Q is S + 2 * P, |
||
cross_out(Q, N, P).</ |
cross_out(Q, N, P).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,206: | Line 2,206: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">DisableDebugger |
||
EnableExplicit |
EnableExplicit |
||
Line 2,284: | Line 2,284: | ||
put(c1,"unsexy primes ending with ...",t1$) |
put(c1,"unsexy primes ending with ...",t1$) |
||
Input()</ |
Input()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>16386 pairs ending with ... |
<pre>16386 pairs ending with ... |
||
Line 2,325: | Line 2,325: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Imperative Style=== |
===Imperative Style=== |
||
< |
<syntaxhighlight lang="python">LIMIT = 1_000_035 |
||
def primes2(limit=LIMIT): |
def primes2(limit=LIMIT): |
||
if limit < 2: return [] |
if limit < 2: return [] |
||
Line 2,380: | Line 2,380: | ||
print(f'\nThere are {len(unsexy)} unsexy primes ending with ...') |
print(f'\nThere are {len(unsexy)} unsexy primes ending with ...') |
||
for usx in unsexy[-10:]: |
for usx in unsexy[-10:]: |
||
print(' ',usx)</ |
print(' ',usx)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,420: | Line 2,420: | ||
{{trans|FSharp}} |
{{trans|FSharp}} |
||
This task uses [[Extensible_prime_generator#210-wheel_postponed_incremental_sieve]] |
This task uses [[Extensible_prime_generator#210-wheel_postponed_incremental_sieve]] |
||
< |
<syntaxhighlight lang="python"> |
||
#Functional Sexy Primes. Nigel Galloway: October 5th., 2018 |
#Functional Sexy Primes. Nigel Galloway: October 5th., 2018 |
||
from itertools import * |
from itertools import * |
||
Line 2,441: | Line 2,441: | ||
print ("There are",len(unsexy),"unsexy primes less than 1,000,035. The last 10 are:") |
print ("There are",len(unsexy),"unsexy primes less than 1,000,035. The last 10 are:") |
||
for g in islice(unsexy,max(len(unsexy)-10,0),len(unsexy)): print(g) |
for g in islice(unsexy,max(len(unsexy)-10,0),len(unsexy)): print(g) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,481: | Line 2,481: | ||
{{works with|Rakudo|2018.08}} |
{{works with|Rakudo|2018.08}} |
||
<lang |
<syntaxhighlight lang="raku" line>use Math::Primesieve; |
||
my $sieve = Math::Primesieve.new; |
my $sieve = Math::Primesieve.new; |
||
Line 2,514: | Line 2,514: | ||
} |
} |
||
sub comma { $^i.flip.comb(3).join(',').flip }</ |
sub comma { $^i.flip.comb(3).join(',').flip }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Total primes less than 1,000,035: 78,500 |
<pre>Total primes less than 1,000,035: 78,500 |
||
Line 2,533: | Line 2,533: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds and displays various kinds of sexy and unsexy primes less than N.*/ |
||
parse arg N endU end2 end3 end4 end5 . /*obtain optional argument from the CL.*/ |
parse arg N endU end2 end3 end4 end5 . /*obtain optional argument from the CL.*/ |
||
if N=='' | N=="," then N= 1000035 - 1 /*Not specified? Then use the default.*/ |
if N=='' | N=="," then N= 1000035 - 1 /*Not specified? Then use the default.*/ |
||
Line 2,620: | Line 2,620: | ||
q=p-18; if \x.q then iterate |
q=p-18; if \x.q then iterate |
||
v=p-24; if \x.v then iterate; x5=x5 v'~'q"~"t'~' || b"~"p |
v=p-24; if \x.v then iterate; x5=x5 v'~'q"~"t'~' || b"~"p |
||
end /*k*/; return</ |
end /*k*/; return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
Line 2,650: | Line 2,650: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
{{Improve|Ring|Does not even ''attempt'' to fulfil the task requirements and has no explanation as to why not}} |
{{Improve|Ring|Does not even ''attempt'' to fulfil the task requirements and has no explanation as to why not}} |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 2,723: | Line 2,723: | ||
next |
next |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,769: | Line 2,769: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang="ruby"> |
|||
<lang Ruby> |
|||
require 'prime' |
require 'prime' |
||
Line 2,797: | Line 2,797: | ||
unsexy.last(10).each {|item| print prime_array[item], " "} |
unsexy.last(10).each {|item| print prime_array[item], " "} |
||
print "\n\n", Time.now - start, " seconds" |
print "\n\n", Time.now - start, " seconds" |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 2,835: | Line 2,835: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">// [dependencies] |
||
// primal = "0.2" |
// primal = "0.2" |
||
// circular-queue = "0.2.5" |
// circular-queue = "0.2.5" |
||
Line 2,902: | Line 2,902: | ||
.collect::<Vec<String>>() |
.collect::<Vec<String>>() |
||
.join(", ") |
.join(", ") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,928: | Line 2,928: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">/* We could reduce the number of functions through a polymorphism since we're trying to retrieve sexy N-tuples (pairs, triplets etc...) |
||
but one practical solution would be to use the Shapeless library for this purpose; here we only use built-in Scala packages. */ |
but one practical solution would be to use the Shapeless library for this purpose; here we only use built-in Scala packages. */ |
||
Line 3,015: | Line 3,015: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,031: | Line 3,031: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var limit = 1e6+35 |
||
var primes = limit.primes |
var primes = limit.primes |
||
Line 3,052: | Line 3,052: | ||
var unsexy_primes = primes.grep {|p| is_prime(p+6) || is_prime(p-6) -> not } |
var unsexy_primes = primes.grep {|p| is_prime(p+6) || is_prime(p-6) -> not } |
||
say "...total number of unsexy primes = #{unsexy_primes.len.commify}" |
say "...total number of unsexy primes = #{unsexy_primes.len.commify}" |
||
say "...where last 10 unsexy primes are: #{unsexy_primes.last(10)}"</ |
say "...where last 10 unsexy primes are: #{unsexy_primes.last(10)}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,078: | Line 3,078: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
import "/math" for Int |
import "/math" for Int |
||
Line 3,140: | Line 3,140: | ||
unwrap.call(printHelper.call("unsexy primes", unsexy.count, lim, 10)) |
unwrap.call(printHelper.call("unsexy primes", unsexy.count, lim, 10)) |
||
System.print("The last %(n) %(verb):\n %(unsexy[le-n..-1])\n")</ |
System.print("The last %(n) %(verb):\n %(unsexy[le-n..-1])\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,166: | Line 3,166: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime |
||
int N, I; |
int N, I; |
||
[if N <= 2 then return N = 2; |
[if N <= 2 then return N = 2; |
||
Line 3,239: | Line 3,239: | ||
[IntOut(0, Unsexy(I)); if I then Text(0, ", ")]; |
[IntOut(0, Unsexy(I)); if I then Text(0, ", ")]; |
||
CrLf(0); |
CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,272: | Line 3,272: | ||
[[Extensible prime generator#zkl]] could be used instead. |
[[Extensible prime generator#zkl]] could be used instead. |
||
< |
<syntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP |
||
const N=1_000_035, M=N+24; // M allows prime group to span N, eg N=100, (97,103) |
const N=1_000_035, M=N+24; // M allows prime group to span N, eg N=100, (97,103) |
||
const OVR=6; // 6 if prime group can NOT span N, else 0 |
const OVR=6; // 6 if prime group can NOT span N, else 0 |
||
Line 3,299: | Line 3,299: | ||
println("Number of %s less than %,d is %,d".fmt(s,N,ps.len())); |
println("Number of %s less than %,d is %,d".fmt(s,N,ps.len())); |
||
println("The last %d %s:\n %s\n".fmt(n, (n>1 and "are" or "is"), gs)); |
println("The last %d %s:\n %s\n".fmt(n, (n>1 and "are" or "is"), gs)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="font-size:80%"> |
<pre style="font-size:80%"> |