Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Ultra_useful_primes

View file

@ -0,0 +1,20 @@
An '''ultra-useful prime''' is a member of the sequence where each <span style="font-size:125%;">'''a(n)'''</span> is the smallest positive integer <span style="font-size:125%;">'''k'''</span> such that <span style="font-size:125%;">'''2<sup>(2<sup>n</sup>)</sup> - k'''</span> is prime.
'''''k''' must always be an odd number since 2 to any power is always even.''
;Task
* Find and show here, on this page, the first '''10''' elements of the sequence.
;Stretch
* Find and show the next several elements. (The numbers get really big really fast. Only nineteen elements have been identified as of this writing.)
;See also
* [[oeis:A058220|OEIS:A058220 - Ultra-useful primes: smallest k such that 2^(2^n) - k is prime]]

View file

@ -0,0 +1,18 @@
BEGIN # find members of the sequence a(n) = smallest k such that 2^(2^n) - k is prime #
PR precision 650 PR # set number of digits for LONG LOMG INT #
# 2^(2^10) has 308 digits but we need more for #
# Miller Rabin primality testing #
PR read "primes.incl.a68" PR # include the prime related utilities #
FOR n TO 10 DO
LONG LONG INT two up 2 up n = LONG LONG INT( 2 ) ^ ( 2 ^ n );
FOR i BY 2
WHILE IF is probably prime( two up 2 up n - i ) THEN
# found a sequence member #
print( ( " ", whole( i, 0 ) ) );
FALSE # stop looking #
ELSE
TRUE # haven't found a sequence member yet #
FI
DO SKIP OD
OD
END

View file

@ -0,0 +1,14 @@
ultraUseful: function [n][
k: 1
p: (2^2^n) - k
while ø [
if prime? p -> return k
p: p-2
k: k+2
]
]
print [pad "n" 3 "|" pad.right "k" 4]
print repeat "-" 10
loop 1..10 'x ->
print [(pad to :string x 3) "|" (pad.right to :string ultraUseful x 4)]

View file

@ -0,0 +1,22 @@
#include <stdio.h>
#include <gmp.h>
int a(unsigned int n) {
int k;
mpz_t p;
mpz_init_set_ui(p, 1);
mpz_mul_2exp(p, p, 1 << n);
mpz_sub_ui(p, p, 1);
for (k = 1; ; k += 2) {
if (mpz_probab_prime_p(p, 15) > 0) return k;
mpz_sub_ui(p, p, 2);
}
}
int main() {
unsigned int n;
printf(" n k\n");
printf("----------\n");
for (n = 1; n < 15; ++n) printf("%2d %d\n", n, a(n));
return 0;
}

View file

@ -0,0 +1,7 @@
USING: io kernel lists lists.lazy math math.primes prettyprint ;
: useful ( -- list )
1 lfrom
[ 2^ 2^ 1 lfrom [ - prime? ] with lfilter car ] lmap-lazy ;
10 useful ltake [ pprint bl ] leach nl

View file

@ -0,0 +1,17 @@
#include "isprime.bas"
Dim As Longint n, k, limit = 10
Dim As ulongint num
For n = 1 To limit
k = -1
Do
k += 2
num = (2 ^ (2 ^ n)) - k
If isPrime(num) Then
Print "n = "; n; " k = "; k
Exit Do
End If
Loop
Next
Sleep

View file

@ -0,0 +1,28 @@
package main
import (
"fmt"
big "github.com/ncw/gmp"
)
var two = big.NewInt(2)
func a(n uint) int {
one := big.NewInt(1)
p := new(big.Int).Lsh(one, 1 << n)
p.Sub(p, one)
for k := 1; ; k += 2 {
if p.ProbablyPrime(15) {
return k
}
p.Sub(p, two)
}
}
func main() {
fmt.Println(" n k")
fmt.Println("----------")
for n := uint(1); n < 14; n++ {
fmt.Printf("%2d %d\n", n, a(n))
}
}

View file

@ -0,0 +1,5 @@
uup=: {{
ref=. 2^2x^y+1
k=. 1
while. -. 1 p: ref-k do. k=. k+2 end.
}}"0

View file

@ -0,0 +1,2 @@
uup i.10
1 3 5 15 5 59 159 189 569 105

View file

@ -0,0 +1,21 @@
import java.math.BigInteger;
public final class UltraUsefulPrimes {
public static void main(String[] args) {
for ( int n = 1; n <= 10; n++ ) {
showUltraUsefulPrime(n);
}
}
private static void showUltraUsefulPrime(int n) {
BigInteger prime = BigInteger.ONE.shiftLeft(1 << n);
BigInteger k = BigInteger.ONE;
while ( ! prime.subtract(k).isProbablePrime(20) ) {
k = k.add(BigInteger.TWO);
}
System.out.print(k + " ");
}
}

View file

@ -0,0 +1,5 @@
using Primes
nearpow2pow2prime(n) = findfirst(k -> isprime(2^(big"2"^n) - k), 1:10000)
@time println([nearpow2pow2prime(n) for n in 1:12])

View file

@ -0,0 +1,15 @@
ClearAll[FindUltraUsefulPrimeK]
FindUltraUsefulPrimeK[n_] := Module[{num, tmp},
num = 2^(2^n);
Do[
If[PrimeQ[num - k],
tmp = k;
Break[];
]
,
{k, 1, \[Infinity], 2}
];
tmp
]
res = FindUltraUsefulPrimeK /@ Range[13];
TableForm[res, TableHeadings -> Automatic]

View file

@ -0,0 +1,17 @@
import std/strformat
import integers
let One = newInteger(1)
echo " n k"
var count = 1
var n = 1
while count <= 13:
var k = 1
var p = One shl (1 shl n) - k
while not p.isPrime:
p -= 2
k += 2
echo &"{n:2} {k}"
inc n
inc count

View file

@ -0,0 +1,19 @@
use strict;
use warnings;
use feature 'say';
use bigint;
use ntheory 'is_prime';
sub useful {
my @n = @_;
my @u;
for my $n (@n) {
my $p = 2**(2**$n);
LOOP: for (my $k = 1; $k < $p; $k += 2) {
is_prime($p-$k) and push @u, $k and last LOOP;
}
}
@u
}
say join ' ', useful 1..13;

View file

@ -0,0 +1,28 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">a</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: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">mpz_sub_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span>
<span style="color: #7060A8;">mpz_sub_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">k</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</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;">"%d "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">64</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">11</span> <span style="color: #008080;">to</span> <span style="color: #000000;">13</span> <span style="color: #008080;">do</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;">"%d "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--

View file

@ -0,0 +1,10 @@
sub useful ($n) {
(|$n).map: {
my $p = 1 +< ( 1 +< $_ );
^$p .first: ($p - *).is-prime
}
}
put useful 1..10;
put useful 11..13;

View file

@ -0,0 +1,23 @@
see "works..." + nl
limit = 10
for n = 1 to limit
k = -1
while true
k = k + 2
num = pow(2,pow(2,n)) - k
if isPrime(num)
? "n = " + n + " k = " + k
exit
ok
end
next
see "done.." + nl
func isPrime num
if (num <= 1) return 0 ok
if (num % 2 = 0 and num != 2) return 0 ok
for i = 3 to floor(num / 2) -1 step 2
if (num % i = 0) return 0 ok
next
return 1

View file

@ -0,0 +1,7 @@
require 'openssl'
(1..10).each do |n|
pow = 2 ** (2 ** n)
print "#{n}:\t"
puts (1..).step(2).detect{|k| OpenSSL::BN.new(pow-k).prime?}
end

View file

@ -0,0 +1,7 @@
say(" n k")
say("----------")
for n in (1..13) {
var t = 2**(2**n)
printf("%2d %d\n", n, {|k| t - k -> is_prob_prime }.first)
}

View file

@ -0,0 +1,27 @@
import math
fn main() {
limit := 10 // depending on computer, higher numbers = longer times
mut num, mut k := i64(0), i64(0)
println("n k\n-------")
for n in 1..limit {
k = -1
for n < limit {
k = k + 2
num = math.powi(2, math.powi(2 , n)) - k
if is_prime(num) == true {
println("${n} ${k}")
break
}
}
}
}
fn is_prime(num i64) bool {
if num <= 1 {return false}
if num % 2 == 0 && num != 2 {return false}
for idx := 3; idx <= math.floor(num / 2) - 1; idx += 2 {
if num % idx == 0 {return false}
}
return true
}

View file

@ -0,0 +1,19 @@
import "./big" for BigInt
import "./fmt" for Fmt
var one = BigInt.one
var two = BigInt.two
var a = Fn.new { |n|
var p = (BigInt.one << (1 << n)) - one
var k = 1
while (true) {
if (p.isProbablePrime(5)) return k
p = p - two
k = k + 2
}
}
System.print(" n k")
System.print("----------")
for (n in 1..10) Fmt.print("$2d $d", n, a.call(n))

View file

@ -0,0 +1,19 @@
import "./gmp" for Mpz
import "./fmt" for Fmt
var one = Mpz.one
var two = Mpz.two
var a = Fn.new { |n|
var p = Mpz.one.lsh(1 << n).sub(one)
var k = 1
while (true) {
if (p.probPrime(15) > 0) return k
p.sub(two)
k = k + 2
}
}
System.print(" n k")
System.print("----------")
for (n in 1..14) Fmt.print("$2d $d", n, a.call(n))