How to resolve the algorithm Truncatable primes step by step in the C# programming language
How to resolve the algorithm Truncatable primes step by step in the C# programming language
Table of Contents
Problem Statement
A truncatable prime is a prime number that when you successively remove digits from one end of the prime, you are left with a new prime number.
The number 997 is called a left-truncatable prime as the numbers 997, 97, and 7 are all prime. The number 7393 is a right-truncatable prime as the numbers 7393, 739, 73, and 7 formed by removing digits from its right are also prime. No zeroes are allowed in truncatable primes.
The task is to find the largest left-truncatable and right-truncatable primes less than one million (base 10 is implied).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Truncatable primes step by step in the C# programming language
The provided C# code is designed to efficiently identify "truncatable primes" within a specified range. Truncatable primes are a unique class of prime numbers that, when truncated from either the left or right side digit by digit, continue to form prime numbers.
The Main()
function initializes a variable m
with a value of 100,000 and prints the results of calling two functions, L(m)
and R(m)
. These functions represent the left and right truncatable prime numbers within the range [2, m
], respectively.
The L(m)
function performs the following operations:
-
It subtracts 1 from
m
and ensures it's an even number (by masking withn & 1
). -
It enters a loop that runs indefinitely, reducing
n
by 2 in each iteration. -
Within the loop, it checks if
n
is divisible by 3, 5, or 7. If it is, it subtracts 2 fromn
. -
It then checks if the last digit of
n
(obtained asn % 10
) is 3 or 7. If it is, it performs a primality test on the digit. If the digit is prime, it continues checking subsequent digits inn
by multiplying a divisord1
by 10 in each iteration. If the test passes for all digits inn
, the function returnsn
as a left truncatable prime.
The R(m)
function, on the other hand, generates right truncatable prime numbers within the range [2, m
]. It starts with a list of primes (p
) and iteratively extends this list by checking if numbers formed by appending digits 1, 3, 7, and 9 to the current primes are prime. It returns the largest prime found in the process.
The isP()
function is used to perform primality tests. It uses a combination of basic checks and modular arithmetic to efficiently determine if a given number is prime.
Overall, this code provides a comprehensive and optimized approach to identify and distinguish left and right truncatable primes within a specified range.
Source code in the csharp programming language
using System; // 4790@3.6
using System.Collections.Generic;
class truncatable_primes
{
static void Main()
{
uint m = 1000000;
Console.Write("L " + L(m) + " R " + R(m) + " ");
var sw = System.Diagnostics.Stopwatch.StartNew();
for (int i = 1000; i > 0; i--) { L(m); R(m); }
Console.Write(sw.Elapsed); Console.Read();
}
static uint L(uint n)
{
n -= n & 1; n--;
for (uint d, d1 = 100; ; n -= 2)
{
while (n % 3 == 0 || n % 5 == 0 || n % 7 == 0) n -= 2;
if ((d = n % 10) == 3 || d == 7)
{
while (d1 < n && d < (d = n % d1) && isP(d)) d1 *= 10;
if (d1 > n && isP(n)) return n; d1 = 100;
}
}
}
static uint R(uint m)
{
var p = new List<uint>() { 2, 3, 5, 7 }; uint n = 20, np;
for (int i = 1; i < p.Count; n = 10 * p[i++])
{
if ((np = n + 1) >= m) break; if (isP(np)) p.Add(np);
if ((np = n + 3) >= m) break; if (isP(np)) p.Add(np);
if ((np = n + 7) >= m) break; if (isP(np)) p.Add(np);
if ((np = n + 9) >= m) break; if (isP(np)) p.Add(np);
}
return p[p.Count - 1];
}
static bool isP(uint n)
{
if (n < 7) return n == 2 || n == 3 || n == 5;
if ((n & 1) == 0 || n % 3 == 0 || n % 5 == 0) return false;
for (uint r = (uint)Math.Sqrt(n), d = 7; d <= r; d += 30)
if (n % (d + 00) == 0 || n % (d + 04) == 0 ||
n % (d + 06) == 0 || n % (d + 10) == 0 ||
n % (d + 12) == 0 || n % (d + 16) == 0 ||
n % (d + 22) == 0 || n % (d + 24) == 0) return false;
return true;
}
}
You may also check:How to resolve the algorithm String length step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Bin given limits step by step in the REXX programming language
You may also check:How to resolve the algorithm Priority queue step by step in the Delphi programming language
You may also check:How to resolve the algorithm Superpermutation minimisation step by step in the Phix programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Perl programming language