Sexy primes: Difference between revisions

m
syntax highlighting fixup automation
(Sexy primes in FreeBASIC)
m (syntax highlighting fixup automation)
Line 32:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V LIMIT = 1'000'000
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)</langsyntaxhighlight>
 
{{out}}
Line 112:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
% 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.</langsyntaxhighlight>
{{out}}
<pre>
Line 267:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SEXY_PRIMES.AWK
BEGIN {
Line 323:
s[key] = str
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 346:
=={{header|C}}==
Similar approach to the Go entry but only stores the arrays that need to be printed out.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 516:
free(sv);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 543:
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <array>
#include <iostream>
#include <vector>
Line 613:
cout << '\n';
return 0;
}</langsyntaxhighlight>
 
Contents of prime_sieve.hpp:
<langsyntaxhighlight lang="cpp">#ifndef PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP
 
Line 667:
}
 
#endif</langsyntaxhighlight>
 
{{out}}
Line 692:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{out}}
<pre>
Line 725:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit fry interpolate io kernel
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</langsyntaxhighlight>
{{out}}
<pre>
Line 789:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Uinteger) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
Line 859:
Next I
Print Chr(8); " "
Sleep</langsyntaxhighlight>
{{out}}
<pre> 78500 primes less than 1000035
Line 892:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
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:])
}</langsyntaxhighlight>
 
{{out}}
Line 1,030:
=={{header|Haskell}}==
Uses Library primes. https://hackage.haskell.org/package/primes (wheel sieve).
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
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"</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="haskell">{-# LANGUAGE TemplateHaskell #-}
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</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">NB. Primes Not Greater Than (the input)
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
)</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight 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
+-----------+-----+----------------------------------------------------------------------+
Line 1,192:
+-----------+-----+----------------------------------------------------------------------+
|Quintuplets|1 |5 11 17 23 29 |
+-----------+-----+----------------------------------------------------------------------+</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
Line 1,269:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,291:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
using Primes
 
Line 1,367:
end
 
primesbysexiness(1000035) </langsyntaxhighlight> {{output}} <pre>
There are:
16386 twins,
Line 1,383:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.2.71
 
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))
}</langsyntaxhighlight>
 
{{output}}
Line 1,489:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">local N = 1000035
 
-- 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(" "))</langsyntaxhighlight>
{{out}}
<pre>16386 pairs, ending with: (999371 999377) (999431 999437) (999721 999727) (999763 999769) (999953 999959)
Line 1,521:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[AllSublengths]
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</langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight Nimlang="nim">import math, strformat, strutils
 
const Lim = 1_000_035
Line 1,667:
printResult(Quadruplets, quads, 5)
printResult(Quintuplets, quints, 5)
printResult(Unsexy, unsexy, 10)</langsyntaxhighlight>
 
{{out}}
Line 1,698:
37907606 unsexy primes // = 50847538-2*6849047+758163-1
It seems so, not a proove.
<langsyntaxhighlight lang="pascal">program SexyPrimes;
 
uses
Line 1,877:
writeln(unsexyprimes,' unsexy primes');
OutLastUnsexy(10);
end.</langsyntaxhighlight>
{{Output}}
<pre>
Line 1,920:
{{libheader|ntheory}}
We will use the prime iterator and primality test from the <code>ntheory</code> module.
<langsyntaxhighlight lang="perl">use ntheory qw/prime_iterator is_prime/;
 
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";</langsyntaxhighlight>
{{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]].
 
<langsyntaxhighlight lang="perl">use ntheory qw/sieve_prime_cluster forprimes is_prime/;
 
# ... identical helper functions
Line 2,007:
} $max-1;
 
# ... identical output code</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,095:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">sexy_prime_group(1, N, _, [N]):-
is_prime(N),
!.
Line 2,141:
 
main:-
main(1000035).</langsyntaxhighlight>
 
Module for finding prime numbers up to some limit:
<langsyntaxhighlight lang="prolog">:- module(prime_numbers, [find_prime_numbers/1, is_prime/1]).
:- dynamic is_prime/1.
 
Line 2,185:
cross_out(S, N, P):-
Q is S + 2 * P,
cross_out(Q, N, P).</langsyntaxhighlight>
 
{{out}}
Line 2,206:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">DisableDebugger
EnableExplicit
 
Line 2,284:
put(c1,"unsexy primes ending with ...",t1$)
 
Input()</langsyntaxhighlight>
{{out}}
<pre>16386 pairs ending with ...
Line 2,325:
=={{header|Python}}==
===Imperative Style===
<langsyntaxhighlight lang="python">LIMIT = 1_000_035
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)</langsyntaxhighlight>
 
{{out}}
Line 2,420:
{{trans|FSharp}}
This task uses [[Extensible_prime_generator#210-wheel_postponed_incremental_sieve]]
<langsyntaxhighlight lang="python">
#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>
</lang>
{{out}}
<pre>
Line 2,481:
{{works with|Rakudo|2018.08}}
 
<syntaxhighlight lang="raku" perl6line>use Math::Primesieve;
my $sieve = Math::Primesieve.new;
 
Line 2,514:
}
 
sub comma { $^i.flip.comb(3).join(',').flip }</langsyntaxhighlight>
{{out}}
<pre>Total primes less than 1,000,035: 78,500
Line 2,533:
 
=={{header|REXX}}==
<langsyntaxhighlight 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.*/
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</langsyntaxhighlight>
{{out|output|text=&nbsp; 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}}
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,723:
next
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,769:
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
<lang Ruby>
require 'prime'
 
Line 2,797:
unsexy.last(10).each {|item| print prime_array[item], " "}
print "\n\n", Time.now - start, " seconds"
</syntaxhighlight>
</lang>
 
Output:
Line 2,835:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.2"
// circular-queue = "0.2.5"
Line 2,902:
.collect::<Vec<String>>()
.join(", ")
}</langsyntaxhighlight>
 
{{out}}
Line 2,928:
 
=={{header|Scala}}==
<langsyntaxhighlight 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. */
 
Line 3,015:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,031:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var limit = 1e6+35
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)}"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,078:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
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")</langsyntaxhighlight>
 
{{out}}
Line 3,166:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 3,239:
[IntOut(0, Unsexy(I)); if I then Text(0, ", ")];
CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 3,272:
 
[[Extensible prime generator#zkl]] could be used instead.
<langsyntaxhighlight 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 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));
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:80%">
10,327

edits