Chernick's Carmichael numbers: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 53:
 
<br><br>
 
=={{header|C}}==
{{libheader|GMP}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
Line 118 ⟶ 117:
a(10) has m = 3208386195840
</pre>
 
=={{header|C++}}==
{{libheader|GMP}}
<syntaxhighlight lang="cpp">#include <gmp.h>
#include <iostream>
 
Line 219 ⟶ 217:
</pre>
(takes ~3.5 minutes)
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<syntaxhighlight lang="fsharp">
// Generate Chernick's Carmichael numbers. Nigel Galloway: June 1st., 2019
let fMk m k=isPrime(6*m+1) && isPrime(12*m+1) && [1..k-2]|>List.forall(fun n->isPrime(9*(pown 2 n)*m+1))
Line 238 ⟶ 235:
cherCar(9): m=950560 primes -> [5703361; 11406721; 17110081; 34220161; 68440321; 136880641; 273761281; 547522561; 1095045121]
</pre>
 
 
=={{header|FreeBASIC}}==
===Basic only===
<syntaxhighlight lang="freebasic">#include "isprime.bas"
 
Function PrimalityPretest(k As Integer) As Boolean
Line 287 ⟶ 282:
Next n
Sleep</syntaxhighlight>
 
 
=={{header|Go}}==
===Basic only===
<syntaxhighlight lang="go">package main
 
import (
Line 369 ⟶ 362:
 
The resulting executable is several hundred times faster than before and, even on my modest Celeron @1.6GHZ, reaches a(9) in under 10ms and a(10) in about 22 minutes.
<syntaxhighlight lang="go">package main
 
import (
Line 499 ⟶ 492:
Factors: [19250317175041 38500634350081 57750951525121 115501903050241 231003806100481 462007612200961 924015224401921 1848030448803841 3696060897607681 7392121795215361]
</pre>
 
=={{header|J}}==
 
Brute force:
 
<syntaxhighlight lang=J"j">a=: {{)v
if.3=y do.1729 return.end.
m=. z=. 2^y-4
Line 517 ⟶ 509:
Task examples:
 
<syntaxhighlight lang=J"j"> a 3
1729
a 4
Line 531 ⟶ 523:
a 9
58571442634534443082821160508299574798027946748324125518533225605795841</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.ArrayList;
Line 739 ⟶ 730:
U(9, 950560) = 5703361 * 11406721 * 17110081 * 34220161 * 68440321 * 136880641 * 273761281 * 547522561 * 1095045121 = 58571442634534443082821160508299574798027946748324125518533225605795841
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Primes
 
function trial_pretest(k::UInt64)
Line 863 ⟶ 853:
 
(takes ~6.5 minutes)
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">ClearAll[PrimeFactorCounts, U]
PrimeFactorCounts[n_Integer] := Total[FactorInteger[n][[All, 2]]]
U[n_, m_] := (6 m + 1) (12 m + 1) Product[2^i 9 m + 1, {i, 1, n - 2}]
Line 899 ⟶ 888:
{950560,53487697914261966820654105730041031613370337776541835775672321}
{950560,58571442634534443082821160508299574798027946748324125518533225605795841}</pre>
 
=={{header|Nim}}==
{{libheader|bignum}}
Line 909 ⟶ 897:
With these optimizations, the program executes in 4-5 minutes.
 
<syntaxhighlight lang=Nim"nim">import strutils, sequtils
import bignum
 
Line 995 ⟶ 983:
a(9) = U(9, 950560) = 5703361 × 11406721 × 17110081 × 34220161 × 68440321 × 136880641 × 273761281 × 547522561 × 1095045121
a(10) = U(10, 3208386195840) = 19250317175041 × 38500634350081 × 57750951525121 × 115501903050241 × 231003806100481 × 462007612200961 × 924015224401921 × 1848030448803841 × 3696060897607681 × 7392121795215361</pre>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">
cherCar(n)={
my(C=vector(n));C[1]=6; C[2]=12; for(g=3,n,C[g]=2^(g-2)*9);
Line 1,017 ⟶ 1,004:
cherCar(10): m = 3208386195840
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use 5.020;
use warnings;
use ntheory qw/:all/;
Line 1,055 ⟶ 1,041:
a(9) = 58571442634534443082821160508299574798027946748324125518533225605795841
</pre>
 
=={{header|Phix}}==
{{libheader|Phix/mpfr}}
{{trans|Sidef}}
<!--<syntaxhighlight lang=Phix"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;">chernick_carmichael_factors</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
Line 1,114 ⟶ 1,099:
{{trans|C}} with added cheat for the a(10) case - I found a nice big prime factor of k and added that on each iteration instead of 1.<br>
You could also use the sequence {1,1,1,1,19,19,4877,457,457,12564169}, if you know a way to build that, and then it wouldn't be cheating anymore...
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,179 ⟶ 1,164:
"0.1s"
</pre>
 
=={{header|Prolog}}==
SWI Prolog is too slow to solve for a(10), even with optimizing by increasing the multiplier and implementing a trial division check. (actually, my implementation of Miller-Rabin in Prolog already starts with a trial division by small primes.)
<syntaxhighlight lang=Prolog"prolog">
?- use_module(library(primality)).
 
Line 1,214 ⟶ 1,198:
</syntaxhighlight>
isprime predicate:
<syntaxhighlight lang=Prolog"prolog">
prime(N) :-
integer(N),
Line 1,272 ⟶ 1,256:
</pre>
=={{header|Python}}==
<syntaxhighlight lang="python">
"""
 
Line 1,351 ⟶ 1,335:
a(9) has m = 950560
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Line 1,358 ⟶ 1,341:
{{trans|Perl}}
{{libheader|ntheory}}
<syntaxhighlight lang="raku" line>use Inline::Perl5;
use ntheory:from<Perl5> <:all>;
 
Line 1,389 ⟶ 1,372:
U(8, 950560): 53487697914261966820654105730041031613370337776541835775672321 = 5703361 ⨉ 11406721 ⨉ 17110081 ⨉ 34220161 ⨉ 68440321 ⨉ 136880641 ⨉ 273761281 ⨉ 547522561
U(9, 950560): 58571442634534443082821160508299574798027946748324125518533225605795841 = 5703361 ⨉ 11406721 ⨉ 17110081 ⨉ 34220161 ⨉ 68440321 ⨉ 136880641 ⨉ 273761281 ⨉ 547522561 ⨉ 1095045121</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func chernick_carmichael_factors (n, m) {
[6*m + 1, 12*m + 1, {|i| 2**i * 9*m + 1 }.map(1 .. n-2)...]
}
Line 1,421 ⟶ 1,403:
a(9) = 5703361 * 11406721 * 17110081 * 34220161 * 68440321 * 136880641 * 273761281 * 547522561 * 1095045121
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
Line 1,427 ⟶ 1,408:
{{libheader|Wren-fmt}}
Based on Go's 'more efficient' version. Reaches a(9) in just over 0.1 seconds but a(10) would still be out of reasonable reach for Wren so I've had to be content with that.
<syntaxhighlight lang=ecmascript"wren">import "./big" for BigInt, BigInts
import "./fmt" for Fmt
 
var min = 3
Line 1,526 ⟶ 1,507:
Using GMP (probabilistic primes),
because it is easy and fast to check primeness.
<syntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
 
fcn ccFactors(n,m){ // not re-entrant
Line 1,554 ⟶ 1,535:
}
}</syntaxhighlight>
<syntaxhighlight lang="zkl">ccNumbers(3,9);</syntaxhighlight>
{{out}}
<pre>
9,476

edits