How to resolve the algorithm Truncatable primes step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Truncatable primes step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

' FB 1.05.0 Win64

Function isPrime(n As Integer) As Boolean
  If n Mod 2 = 0 Then Return n = 2
  If n Mod 3 = 0 Then Return n = 3
  Dim d As Integer = 5
  While d * d <= n
    If n Mod d = 0 Then Return False
    d += 2
    If n Mod d = 0 Then Return False
    d += 4
  Wend
  Return True
End Function

Dim As UInteger i, j, p, pow, lMax = 2, rMax = 2 
Dim s As String

' largest left truncatable prime less than 1000000
' It can't end with 1, 4, 6, 8 or 9 as these numbers are not prime
' Nor can it end in 2 if it has more than one digit as such a number would divide by 2
For i = 3 To 999997 Step 2
  s = Str(i)
  If Instr(s, "0") > 1 Then Continue For '' cannot contain 0   
  j = s[Len(s) - 1] - 48
  If j = 1 OrElse j = 9 Then Continue For
  p = i
  pow = 10 ^ (Len(s) - 1)
  While pow > 1
    If Not isPrime(p) Then Continue For
    p Mod= pow
    pow \= 10
  Wend
  lMax = i
Next

' largest right truncatable prime less than 1000000
' It can't begin with 1, 4, 6, 8 or 9 as these numbers are not prime
For i = 3 To 799999 Step 2
  s = Str(i)
  If Instr(s, "0") > 1 Then Continue For '' cannot contain 0   
  j = s[0] - 48
  If j = 1 OrElse j = 4 OrElse j = 6 Then Continue For
  p = i
  While p > 0 
    If Not isPrime(p) Then Continue For  
    p \= 10
  Wend
  rMax = i
Next

Print "Largest left  truncatable prime : "; lMax
Print "Largest right truncatable prime : "; rMax
Print
Print "Press any key to quit"
Sleep

' version 10-12-2016
' compile with: fbc -s console

Dim Shared As Byte isPrime()

Sub sieve(m As UInteger)

    Dim As Integer i, j
    ReDim isPrime(m)

    For i = 4 To m Step 2
        isPrime(i) = 1
    Next

    For i = 3 To Sqr(m) Step 2
        If isPrime(i) = 0 Then
            For j = i * i To m Step i * 2
                isPrime(j) = 1
            Next
        End If
    Next

End Sub

' ------=< MAIN >=------

#Define max 1000000 'upto 2^30 max for 32bit OS

Dim As UInteger a(), lt_prime(5000), rt_prime(100)
Dim As UInteger i, j, j1, p1, p2, left_max, right_max

sieve(max)

' left truncatable primes
' if odd and ends with 3 or 7, never ends 1 or 9 (no prime
' never ends on a 2 or 5 and starts with 1 to 9
lt_prime(1) = 3 : lt_prime(2) = 7
p1 = 1 : p2 = 2

Do
    For i = 1 To 9
        j = Val( Str(i) + Str(lt_prime(p1)) )
        If j > max Then Exit Do
        If isPrime(j) = 0 Then ' if prime then add to the list
            p2 += 1
            lt_prime(p2) = j
            If Left_max < j Then left_max = j
        End If
    Next
    p1 += 1
Loop Until p1 > p2   ' no more numbers to process

' right truncatable prime
' start with 2, 3, 5 or 7 and end with 1, 3, 7 or 9
rt_prime(1) = 2 : rt_prime(2) = 3 : rt_prime(3) = 5 : rt_prime(4) = 7
p1 = 1 : p2 = 4
Dim As UInteger end_num(1 To 4) => {1, 3, 7, 9}

Do
    j1 = rt_prime(p1) * 10
    If j1 > max Then Exit Do
    For i = 1 To 4
        j = j1 + End_num(i)
        If isprime(j) = 0 Then  ' if prime then add to the list
            p2 += 1
            rt_prime(p2) = j
           ' If right_max < j Then right_max = j
        End If
    Next
    p1 += 1
Loop Until p1 > p2   ' no more numbers to process
' the last one added is the biggest
right_max = rt_prime(p2)

Print
Print "The biggest  left truncatable prime below"; max; " is "; left_max
Print "The biggest right truncatable prime below"; max; " is "; right_max

' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

  

You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Totient function step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Playing cards step by step in the FreeBASIC programming language