Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
|
|
@ -20,5 +20,6 @@ BEGIN # find additive primes - primes whose digit sum is also prime #
|
|||
FI
|
||||
FI
|
||||
OD;
|
||||
print( ( newline, "Found ", whole( additive count, 0 ), " additive primes below ", whole( UPB prime + 1, 0 ), newline ) )
|
||||
print( ( newline, "Found ", whole( additive count, 0 ) ) );
|
||||
print( ( " additive primes below ", whole( UPB prime + 1, 0 ), newline ) )
|
||||
END
|
||||
|
|
|
|||
86
Task/Additive-primes/C-sharp/additive-primes.cs
Normal file
86
Task/Additive-primes/C-sharp/additive-primes.cs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
long primeCandidate = 1;
|
||||
long additivePrimeCount = 0;
|
||||
Console.WriteLine("Additive Primes");
|
||||
|
||||
while (primeCandidate < 500)
|
||||
{
|
||||
if (IsAdditivePrime(primeCandidate))
|
||||
{
|
||||
additivePrimeCount++;
|
||||
|
||||
Console.Write($"{primeCandidate,-3} ");
|
||||
|
||||
if (additivePrimeCount % 10 == 0)
|
||||
{
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
primeCandidate++;
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Found {additivePrimeCount} additive primes less than 500");
|
||||
}
|
||||
|
||||
private static bool IsAdditivePrime(long number)
|
||||
{
|
||||
if (IsPrime(number) && IsPrime(DigitSum(number)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsPrime(long number)
|
||||
{
|
||||
if (number < 2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (number % 2 == 0)
|
||||
{
|
||||
return number == 2;
|
||||
}
|
||||
|
||||
if (number % 3 == 0)
|
||||
{
|
||||
return number == 3;
|
||||
}
|
||||
|
||||
int delta = 2;
|
||||
long k = 5;
|
||||
|
||||
while (k * k <= number)
|
||||
{
|
||||
if (number % k == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
k += delta;
|
||||
delta = 6 - delta;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static long DigitSum(long n)
|
||||
{
|
||||
long sum = 0;
|
||||
|
||||
while (n > 0)
|
||||
{
|
||||
sum += n % 10;
|
||||
n /= 10;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +1,20 @@
|
|||
val .isPrime = fn(.i) .i == 2 or .i > 2 and
|
||||
not any fn(.x) .i div .x, pseries 2 .. .i ^/ 2
|
||||
val isPrime = fn(i) {
|
||||
i == 2 or i > 2 and
|
||||
not any(fn x: i div x, pseries(2 .. i ^/ 2))
|
||||
}
|
||||
|
||||
val .sumDigits = fn(.i) fold fn{+}, s2n string .i
|
||||
val sumDigits = fn i: fold(fn{+}, s2n(string(i)))
|
||||
|
||||
writeln "Additive primes less than 500:"
|
||||
|
||||
var .count = 0
|
||||
var cnt = 0
|
||||
|
||||
for .i in [2] ~ series(3..500, 2) {
|
||||
if .isPrime(.i) and .isPrime(.sumDigits(.i)) {
|
||||
write $"\{.i:3} "
|
||||
.count += 1
|
||||
if .count div 10: writeln()
|
||||
for i in [2] ~ series(3..500, 2) {
|
||||
if isPrime(i) and isPrime(sumDigits(i)) {
|
||||
write "{{i:3}} "
|
||||
cnt += 1
|
||||
if cnt div 10: writeln()
|
||||
}
|
||||
}
|
||||
|
||||
writeln $"\n\n\{.count} additive primes found.\n"
|
||||
writeln "\n\n{{cnt}} additive primes found.\n"
|
||||
|
|
|
|||
44
Task/Additive-primes/Modula-3/additive-primes.mod3
Normal file
44
Task/Additive-primes/Modula-3/additive-primes.mod3
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
MODULE AdditivePrimes EXPORTS Main;
|
||||
|
||||
IMPORT SIO,Fmt;
|
||||
|
||||
CONST
|
||||
Max = 500;
|
||||
|
||||
VAR
|
||||
Count:CARDINAL := 0;
|
||||
Prime:ARRAY[2..Max] OF BOOLEAN;
|
||||
|
||||
PROCEDURE DigitSum(N:CARDINAL):CARDINAL =
|
||||
BEGIN
|
||||
IF N < 10 THEN RETURN N
|
||||
ELSE RETURN (N MOD 10) + DigitSum(N DIV 10) END;
|
||||
END DigitSum;
|
||||
|
||||
PROCEDURE Sieve() =
|
||||
VAR J:CARDINAL;
|
||||
BEGIN
|
||||
FOR I := 2 TO Max DO Prime[I] := TRUE END;
|
||||
FOR I := 2 TO Max DIV 2 DO
|
||||
IF Prime[I] THEN
|
||||
J := I*2;
|
||||
WHILE J <= Max DO
|
||||
Prime[J] := FALSE;
|
||||
INC(J,I)
|
||||
END
|
||||
END
|
||||
END;
|
||||
END Sieve;
|
||||
|
||||
BEGIN
|
||||
Sieve();
|
||||
FOR N := 2 TO Max DO
|
||||
IF Prime[N] AND Prime[DigitSum(N)] THEN
|
||||
SIO.PutText(Fmt.F("%4s",Fmt.Int(N)));
|
||||
INC(Count);
|
||||
IF Count MOD 10 = 0 THEN SIO.Nl() END
|
||||
END
|
||||
END;
|
||||
SIO.PutText(Fmt.F("\nThere are %s additive primes less than %s.\n",
|
||||
Fmt.Int(Count),Fmt.Int(Max)));
|
||||
END AdditivePrimes.
|
||||
Loading…
Add table
Add a link
Reference in a new issue