Sexy primes: Difference between revisions
m
syntax highlighting fixup automation
(Sexy primes in FreeBASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 32:
{{trans|Python}}
<
F get_primes(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
Line 72:
print("\nThere are #. unsexy primes ending with ...".format(unsexy.len))
L(usx) unsexy[(len)-10..]
print(‘ ’usx)</
{{out}}
Line 112:
=={{header|ALGOL W}}==
<
% find some sexy primes - primes that differ from another prime by 6 %
% implements the sieve of Eratosthenes %
Line 251:
showPrimes( 1, unsexy, 10, "unsexy primes", MAX_SEXY )
end
end.</
{{out}}
<pre>
Line 267:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SEXY_PRIMES.AWK
BEGIN {
Line 323:
s[key] = str
}
</syntaxhighlight>
{{out}}
<pre>
Line 346:
=={{header|C}}==
Similar approach to the Go entry but only stores the arrays that need to be printed out.
<
#include <stdlib.h>
#include <string.h>
Line 516:
free(sv);
return 0;
}</
{{out}}
Line 543:
=={{header|C++}}==
{{libheader|Boost}}
<
#include <iostream>
#include <vector>
Line 613:
cout << '\n';
return 0;
}</
Contents of prime_sieve.hpp:
<
#define PRIME_SIEVE_HPP
Line 667:
}
#endif</
{{out}}
Line 692:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<
// 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
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
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>
{{out}}
<pre>
Line 725:
=={{header|Factor}}==
<
literals locals make math math.primes math.ranges prettyprint qw
sequences tools.memory.private ;
Line 768:
: main ( -- ) 2 5 [a,b] [ show-tuplets ] each show-unsexy ;
MAIN: main</
{{out}}
<pre>
Line 789:
=={{header|FreeBASIC}}==
<
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
Line 859:
Next I
Print Chr(8); " "
Sleep</
{{out}}
<pre> 78500 primes less than 1000035
Line 892:
=={{header|Go}}==
<
import "fmt"
Line 1,004:
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:])
}</
{{out}}
Line 1,030:
=={{header|Haskell}}==
Uses Library primes. https://hackage.haskell.org/package/primes (wheel sieve).
<
import Data.Numbers.Primes (isPrime, primes)
Line 1,066:
where (pairs, triplets, quads, quins, unsexy) = foldr groups ([], [], [], [], []) $ takeWhile (< 1000035) primes
lastFive xs = show $ drop (length xs - 5) xs
lastFiveText = " Last 5 : %s\n\n"</
{{out}}
<pre>
Line 1,085:
</pre>
Slight variation which only holds on to the display results. Does not perform any better than above though. Both run ~ 250ms.
<
import Control.Lens (makeLenses, over, (^.), to, view)
import Data.Numbers.Primes (isPrime, primes)
Line 1,134:
collectGroups = foldl collect initialGroups . takeWhile (< 1000035)
display :: Show a => [a] -> String
display = show . reverse</
=={{header|J}}==
<
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
Line 1,163:
us =. p (] #~ 1 +:/@:p: +/)~ (+,-) pd NB. Unsexy numbers
( (# ; _10&{.) us ) , (#&.> g) ,. l
)</
{{out}}
<
(;:'Group Count Examples') , (;:'Unsexy Pairs Triplets Quadruplets Quintuplets') ,. r
+-----------+-----+----------------------------------------------------------------------+
Line 1,192:
+-----------+-----+----------------------------------------------------------------------+
|Quintuplets|1 |5 11 17 23 29 |
+-----------+-----+----------------------------------------------------------------------+</
=={{header|Java}}==
<
import java.util.ArrayList;
import java.util.List;
Line 1,269:
}
</syntaxhighlight>
{{out}}
Line 1,291:
=={{header|Julia}}==
<
using Primes
Line 1,367:
end
primesbysexiness(1000035) </
There are:
16386 twins,
Line 1,383:
=={{header|Kotlin}}==
{{trans|Go}}
<
fun sieve(lim: Int): BooleanArray {
Line 1,462:
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))
}</
{{output}}
Line 1,489:
=={{header|Lua}}==
<
-- FUNCS:
Line 1,512:
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(" "))</
{{out}}
<pre>16386 pairs, ending with: (999371 999377) (999431 999437) (999721 999727) (999763 999769) (999953 999959)
Line 1,521:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
AllSublengths[l_List] := If[Length[l] > 2,
Catenate[Partition[l, #, 1] & /@ Range[2, Length[l]]]
Line 1,551:
sel // Column
Select[Complement[primes, DeleteDuplicates[Catenate@sp]][[-20 ;;]], ! (PrimeQ[# + 6] \[Or] PrimeQ[# - 6]) &][[-10 ;;]] // Column</
{{out}}
<pre>16386
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.
<
const Lim = 1_000_035
Line 1,667:
printResult(Quadruplets, quads, 5)
printResult(Quintuplets, quints, 5)
printResult(Unsexy, unsexy, 10)</
{{out}}
Line 1,698:
37907606 unsexy primes // = 50847538-2*6849047+758163-1
It seems so, not a proove.
<
uses
Line 1,877:
writeln(unsexyprimes,' unsexy primes');
OutLastUnsexy(10);
end.</
{{Output}}
<pre>
Line 1,920:
{{libheader|ntheory}}
We will use the prime iterator and primality test from the <code>ntheory</code> module.
<
sub tuple_tail {
Line 1,966:
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";</
{{out}}
<pre>Total primes less than 1,000,035: 78,500
Line 1,990:
The cluster sieve becomes more efficient as the number of terms increases. See for example [[oeis:a213646|OEIS Prime 11-tuplets]].
<
# ... identical helper functions
Line 2,007:
} $max-1;
# ... identical output code</
=={{header|Phix}}==
<!--<
<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>
Line 2,082:
<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>
<!--</
{{out}}
<pre>
Line 2,095:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
is_prime(N),
!.
Line 2,141:
main:-
main(1000035).</
Module for finding prime numbers up to some limit:
<
:- dynamic is_prime/1.
Line 2,185:
cross_out(S, N, P):-
Q is S + 2 * P,
cross_out(Q, N, P).</
{{out}}
Line 2,206:
=={{header|PureBasic}}==
<
EnableExplicit
Line 2,284:
put(c1,"unsexy primes ending with ...",t1$)
Input()</
{{out}}
<pre>16386 pairs ending with ...
Line 2,325:
=={{header|Python}}==
===Imperative Style===
<
def primes2(limit=LIMIT):
if limit < 2: return []
Line 2,380:
print(f'\nThere are {len(unsexy)} unsexy primes ending with ...')
for usx in unsexy[-10:]:
print(' ',usx)</
{{out}}
Line 2,420:
{{trans|FSharp}}
This task uses [[Extensible_prime_generator#210-wheel_postponed_incremental_sieve]]
<
#Functional Sexy Primes. Nigel Galloway: October 5th., 2018
from itertools import *
Line 2,441:
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)
</syntaxhighlight>
{{out}}
<pre>
Line 2,481:
{{works with|Rakudo|2018.08}}
<syntaxhighlight lang="raku"
my $sieve = Math::Primesieve.new;
Line 2,514:
}
sub comma { $^i.flip.comb(3).join(',').flip }</
{{out}}
<pre>Total primes less than 1,000,035: 78,500
Line 2,533:
=={{header|REXX}}==
<
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.*/
Line 2,620:
q=p-18; if \x.q then iterate
v=p-24; if \x.v then iterate; x5=x5 v'~'q"~"t'~' || b"~"p
end /*k*/; return</
{{out|output|text= when using the default inputs:}}
Line 2,650:
=={{header|Ring}}==
{{Improve|Ring|Does not even ''attempt'' to fulfil the task requirements and has no explanation as to why not}}
<
load "stdlib.ring"
Line 2,723:
next
next
</syntaxhighlight>
Output:
<pre>
Line 2,769:
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
require 'prime'
Line 2,797:
unsexy.last(10).each {|item| print prime_array[item], " "}
print "\n\n", Time.now - start, " seconds"
</syntaxhighlight>
Output:
Line 2,835:
=={{header|Rust}}==
<
// primal = "0.2"
// circular-queue = "0.2.5"
Line 2,902:
.collect::<Vec<String>>()
.join(", ")
}</
{{out}}
Line 2,928:
=={{header|Scala}}==
<
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:
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,031:
=={{header|Sidef}}==
<
var primes = limit.primes
Line 3,052:
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 "...where last 10 unsexy primes are: #{unsexy_primes.last(10)}"</
{{out}}
<pre>
Line 3,078:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<
import "/math" for Int
Line 3,140:
unwrap.call(printHelper.call("unsexy primes", unsexy.count, lim, 10))
System.print("The last %(n) %(verb):\n %(unsexy[le-n..-1])\n")</
{{out}}
Line 3,166:
=={{header|XPL0}}==
<
int N, I;
[if N <= 2 then return N = 2;
Line 3,239:
[IntOut(0, Unsexy(I)); if I then Text(0, ", ")];
CrLf(0);
]</
{{out}}
Line 3,272:
[[Extensible prime generator#zkl]] could be used instead.
<
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
Line 3,299:
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));
}</
{{out}}
<pre style="font-size:80%">
|