Data update
This commit is contained in:
parent
ed705008a8
commit
0df55f9f24
2196 changed files with 32999 additions and 3075 deletions
40
Task/Fortunate-numbers/ALGOL-68/fortunate-numbers.alg
Normal file
40
Task/Fortunate-numbers/ALGOL-68/fortunate-numbers.alg
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
BEGIN # find some Fortunate numbers m, m is smallest positive integer > 1 #
|
||||
# where primorial(n) + m is prime for some n #
|
||||
# as all primorials are even, m must be odd #
|
||||
|
||||
PR precision 2000 PR # set the number of digits for LONG LONG INT #
|
||||
PR read "primes.incl.a68" PR # include prime utilities #
|
||||
INT max fortunate = 500; # largeest fortunate number we will consider #
|
||||
[]BOOL is prime = PRIMESIEVE 5 000;
|
||||
[ 1 : max fortunate ]INT fortunate; FOR i TO max fortunate DO fortunate[ i ] := 0 OD;
|
||||
INT primorial pos := 0;
|
||||
LONG LONG INT primorial := 1;
|
||||
INT prime pos := 0;
|
||||
WHILE primorial pos < 100 DO
|
||||
WHILE NOT is prime[ prime pos +:= 1 ] DO SKIP OD;
|
||||
primorial pos +:= 1;
|
||||
primorial *:= prime pos;
|
||||
INT m := 3;
|
||||
WHILE NOT is probably prime( primorial + m ) AND m <= max fortunate DO m +:= 2 OD;
|
||||
IF m <= max fortunate THEN
|
||||
IF fortunate[ m ] = 0 THEN fortunate[ m ] := primorial pos FI
|
||||
FI
|
||||
OD;
|
||||
print( ( "The first 50 Fortunate numbers:", newline ) );
|
||||
INT f count := 0;
|
||||
FOR f TO max fortunate WHILE f count < 50 DO
|
||||
IF fortunate[ f ] /= 0 THEN
|
||||
print( ( whole( f, -5 ) ) );
|
||||
IF ( f count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
|
||||
FI
|
||||
OD;
|
||||
print( ( "The primorial associated with the first 50 Fortunate numbers:", newline ) );
|
||||
f count := 0;
|
||||
FOR f TO max fortunate WHILE f count < 50 DO
|
||||
IF fortunate[ f ] /= 0 THEN
|
||||
print( ( whole( fortunate[ f ], -5 ) ) );
|
||||
IF ( f count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
|
||||
FI
|
||||
OD
|
||||
|
||||
END
|
||||
118
Task/Fortunate-numbers/C-sharp/fortunate-numbers.cs
Normal file
118
Task/Fortunate-numbers/C-sharp/fortunate-numbers.cs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
|
||||
public class FortunateNumbers
|
||||
{
|
||||
private const int CERTAINTY_LEVEL = 10;
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var primes = PrimeSieve(400);
|
||||
SortedSet<int> fortunates = new SortedSet<int>();
|
||||
BigInteger primorial = BigInteger.One;
|
||||
|
||||
foreach (var prime in primes)
|
||||
{
|
||||
primorial *= prime;
|
||||
int candidate = 3;
|
||||
while (!BigInteger.Add(primorial, candidate).IsProbablyPrime(CERTAINTY_LEVEL))
|
||||
{
|
||||
candidate += 2;
|
||||
}
|
||||
fortunates.Add(candidate);
|
||||
}
|
||||
|
||||
Console.WriteLine("The first 50 distinct fortunate numbers are:");
|
||||
int count = 0;
|
||||
foreach (var fortunate in fortunates)
|
||||
{
|
||||
if (count >= 50) break;
|
||||
Console.Write($"{fortunate,4}{(count % 10 == 9 ? "\n" : "")}");
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
private static List<int> PrimeSieve(int aNumber)
|
||||
{
|
||||
var sieve = new bool[aNumber + 1];
|
||||
var primes = new List<int>();
|
||||
|
||||
for (int i = 2; i <= aNumber; i++)
|
||||
{
|
||||
if (!sieve[i])
|
||||
{
|
||||
primes.Add(i);
|
||||
for (int j = i * i; j <= aNumber && j > 0; j += i)
|
||||
{
|
||||
sieve[j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BigIntegerExtensions
|
||||
{
|
||||
private static Random random = new Random();
|
||||
|
||||
public static bool IsProbablyPrime(this BigInteger source, int certainty)
|
||||
{
|
||||
if (source == 2 || source == 3)
|
||||
return true;
|
||||
if (source < 2 || source % 2 == 0)
|
||||
return false;
|
||||
|
||||
BigInteger d = source - 1;
|
||||
int s = 0;
|
||||
|
||||
while (d % 2 == 0)
|
||||
{
|
||||
d /= 2;
|
||||
s += 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < certainty; i++)
|
||||
{
|
||||
BigInteger a = RandomBigInteger(2, source - 2);
|
||||
BigInteger x = BigInteger.ModPow(a, d, source);
|
||||
if (x == 1 || x == source - 1)
|
||||
continue;
|
||||
|
||||
for (int r = 1; r < s; r++)
|
||||
{
|
||||
x = BigInteger.ModPow(x, 2, source);
|
||||
if (x == 1)
|
||||
return false;
|
||||
if (x == source - 1)
|
||||
break;
|
||||
}
|
||||
|
||||
if (x != source - 1)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static BigInteger RandomBigInteger(BigInteger minValue, BigInteger maxValue)
|
||||
{
|
||||
if (minValue > maxValue)
|
||||
throw new ArgumentException("minValue must be less than or equal to maxValue");
|
||||
|
||||
BigInteger range = maxValue - minValue + 1;
|
||||
int length = range.ToByteArray().Length;
|
||||
byte[] buffer = new byte[length];
|
||||
|
||||
BigInteger result;
|
||||
do
|
||||
{
|
||||
random.NextBytes(buffer);
|
||||
buffer[buffer.Length - 1] &= 0x7F; // Ensure non-negative
|
||||
result = new BigInteger(buffer);
|
||||
} while (result < minValue || result >= maxValue);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue