Cubic special primes: Difference between revisions
Line 87:
<pre>
Same as Wren example.
</pre>
=={{header|Phix}}==
<!--<lang Phix>-->
<span style="color: #004080;">atom</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">=</span><span style="color: #000000;">16000</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">desc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"linear quadratic cubic quartic quintic sextic septic"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">lastn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">p</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lastn</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">></span><span style="color: #000000;">N</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">m</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%5d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">res</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</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;">"Found %d %s special primes < %,d:\n%s\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><span style="color: #000000;">desc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">],</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
{{out}}
<pre>
Found 23 cubic special primes < 16,000:
2 3 11 19 83 1811 2027 2243
2251 2467 2531 2539 3539 3547 4547 5059
10891 12619 13619 13627 13691 13907 14419
Found 3 quartic special primes < 16,000:
2 3 19
Found 2 quintic special primes < 16,000:
2 3
Found 4 sextic special primes < 16,000:
2 3 67 131
Found 3 septic special primes < 16,000:
2 3 131
</pre>
|
Revision as of 12:07, 29 March 2021
- Task
n is smallest prime such that the difference of successive terms are the smallest cubics of positive integers,
where n < 15000.
Go
<lang go>package main
import (
"fmt" "math"
)
func sieve(limit int) []bool {
limit++ // True denotes composite, false denotes prime. c := make([]bool, limit) // all false by default c[0] = true c[1] = true // no need to bother with even numbers over 2 for this task p := 3 // Start from 3. for { p2 := p * p if p2 >= limit { break } for i := p2; i < limit; i += 2 * p { c[i] = true } for { p += 2 if !c[p] { break } } } return c
}
func isCube(n int) bool {
s := int(math.Cbrt(float64(n))) return s*s*s == n
}
func commas(n int) string {
s := fmt.Sprintf("%d", n) if n < 0 { s = s[1:] } le := len(s) for i := le - 3; i >= 1; i -= 3 { s = s[0:i] + "," + s[i:] } if n >= 0 { return s } return "-" + s
}
func main() {
c := sieve(14999) fmt.Println("Cubic special primes under 15,000:") fmt.Println(" Prime1 Prime2 Gap Cbrt") lastCubicSpecial := 3 gap := 1 count := 1 fmt.Printf("%7d %7d %6d %4d\n", 2, 3, 1, 1) for i := 5; i < 15000; i += 2 { if c[i] { continue } gap = i - lastCubicSpecial if isCube(gap) { cbrt := int(math.Cbrt(float64(gap))) fmt.Printf("%7s %7s %6s %4d\n", commas(lastCubicSpecial), commas(i), commas(gap), cbrt) lastCubicSpecial = i count++ } } fmt.Println("\n", count+1, "such primes found.")
}</lang>
- Output:
Same as Wren example.
Phix
atom N=16000 sequence desc = split("linear quadratic cubic quartic quintic sextic septic") for p=3 to length(desc) do integer lastn = ceil(power(N,1/p)) sequence res = {2} bool done = false while not done do for n=1 to lastn do atom m = res[$] + power(n,p) if m>N then done = true exit elsif is_prime(m) then res &= m exit end if end for end while string r = join_by(apply(true,sprintf,{{"%5d"},res}),1,8) printf(1,"Found %d %s special primes < %,d:\n%s\n",{length(res),desc[p],N,r}) end for
- Output:
Found 23 cubic special primes < 16,000: 2 3 11 19 83 1811 2027 2243 2251 2467 2531 2539 3539 3547 4547 5059 10891 12619 13619 13627 13691 13907 14419 Found 3 quartic special primes < 16,000: 2 3 19 Found 2 quintic special primes < 16,000: 2 3 Found 4 sextic special primes < 16,000: 2 3 67 131 Found 3 septic special primes < 16,000: 2 3 131
Raku
A two character difference from the Quadrat Special Primes entry. (And it could have been one.) <lang perl6>my @sqp = 2, -> $previous {
my $next; for (1..∞).map: *³ { $next = $previous + $_; last if $next.is-prime; } $next
} … *;
say "{+$_} matching numbers:\n", $_».fmt('%5d').batch(7).join: "\n" given
@sqp[^(@sqp.first: * > 15000, :k)];</lang>
- Output:
23 matching numbers: 2 3 11 19 83 1811 2027 2243 2251 2467 2531 2539 3539 3547 4547 5059 10891 12619 13619 13627 13691 13907 14419
Ring
<lang ring> load "stdlib.ring"
see "working..." + nl
Primes = [] limit1 = 50 oldPrime = 2 add(Primes,2)
for n = 1 to limit1
nextPrime = oldPrime + pow(n,3) if isprime(nextPrime) n = 1 add(Primes,nextPrime) oldPrime = nextPrime else nextPrime = nextPrime - oldPrime ok
next
see "prime1 prime2 Gap Cbrt" + nl for n = 1 to Len(Primes)-1
diff = Primes[n+1] - Primes[n] for m = 1 to diff if pow(m,3) = diff cbrt = m exit ok next see ""+ Primes[n] + " " + Primes[n+1] + " " + diff + " " + cbrt + nl
next
see "Found " + Len(Primes) + " of the smallest primes < 15,000 such that the difference of successive terma are the smallest cubic numbers" + nl
see "done..." + nl </lang>
- Output:
working... prime1 prime2 Gap Cbrt 2 3 1 1 3 11 8 2 11 19 8 2 19 83 64 4 83 1811 1728 12 1811 2027 216 6 2027 2243 216 6 2243 2251 8 2 2251 2467 216 6 2467 2531 64 4 2531 2539 8 2 2539 3539 1000 10 3539 3547 8 2 3547 4547 1000 10 4547 5059 512 8 5059 10891 5832 18 10891 12619 1728 12 12619 13619 1000 10 13619 13627 8 2 13627 13691 64 4 13691 13907 216 6 13907 14419 512 8 Found 23 of the smallest primes < 15,000 such that the difference of successive terma are the smallest cubic numbers done...
Wren
<lang ecmascript>import "/math" for Int, Math import "/fmt" for Fmt
var isCube = Fn.new { |n|
var c = Math.cbrt(n).round return c*c*c == n
}
var primes = Int.primeSieve(14999) System.print("Cubic special primes under 15,000:") System.print(" Prime1 Prime2 Gap Cbrt") var lastCubicSpecial = 3 var gap = 1 var count = 1 Fmt.print("$,7d $,7d $,6d $4d", 2, 3, 1, 1) for (p in primes.skip(2)) {
gap = p - lastCubicSpecial if (isCube.call(gap)) { Fmt.print("$,7d $,7d $,6d $4d", lastCubicSpecial, p, gap, Math.cbrt(gap).round) lastCubicSpecial = p count = count + 1 }
} System.print("\n%(count+1) such primes found.")</lang>
- Output:
Cubic special primes under 15,000: Prime1 Prime2 Gap Cbrt 2 3 1 1 3 11 8 2 11 19 8 2 19 83 64 4 83 1,811 1,728 12 1,811 2,027 216 6 2,027 2,243 216 6 2,243 2,251 8 2 2,251 2,467 216 6 2,467 2,531 64 4 2,531 2,539 8 2 2,539 3,539 1,000 10 3,539 3,547 8 2 3,547 4,547 1,000 10 4,547 5,059 512 8 5,059 10,891 5,832 18 10,891 12,619 1,728 12 12,619 13,619 1,000 10 13,619 13,627 8 2 13,627 13,691 64 4 13,691 13,907 216 6 13,907 14,419 512 8 23 such primes found.