How to resolve the algorithm Prime numbers whose neighboring pairs are tetraprimes step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Prime numbers whose neighboring pairs are tetraprimes step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

The following definitions are needed for this task. A tetraprime is a positive integer which is the product of four distinct primes. For example, 1155 is a tetraprime because 1155 = 3 x 5 x 7 x 11. The neighboring pairs of a prime are the two consecutive numbers immediately preceding or immediately following that prime. For example, (5, 6) and (8, 9) are the neighboring pairs of the prime 7.

  1. Find and show here all primes less than 100,000 whose preceding neighboring pair are both tetraprimes.
  2. Find and show here all primes less than 100,000 whose following neighboring pair are both tetraprimes.
  3. Of the primes in 1 and 2 above how many have a neighboring pair one of whose members has a prime factor of 7?
  4. For the primes in 1 and 2 above, consider the gaps between consecutive primes. What are the minimum, median and maximum gaps?
  5. Repeat these calculations for all primes less than 1 million but for 1 and 2 just show the number of primes - don't print them out individually. If it is difficult for your language to meet all of these requirements, then just do what you reasonably can.

Repeat the calculations for all primes less than 10 million.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Prime numbers whose neighboring pairs are tetraprimes step by step in the FreeBASIC programming language

Source code in the freebasic programming language

#include "isprime.bas"

Dim Shared As Boolean Have7              'A tetraprime factor is 7

Function isTetraprime(n As Integer) As Boolean
    Dim As Boolean distinto
    Dim As Integer div = 2, count = 0
    While n >= div*div
        distinto = True
        While n Mod div = 0
            If Not distinto Then Return False
            distinto = False
            count += 1
            If div = 7 Then Have7 = True
            n /= div
        Wend
        div += 1
    Wend
    If n > 1 Then count += 1
    Return count = 4
End Function

Dim As Integer signo = -1
Dim As Integer TenPower = 1e5
Dim As Integer f, g, n, m, count, count7
Dim As Integer Gap, GapMin, GapMax, GapSum
For f = 5 To 7
    For g = 1 To 2       'preceding or following neighboring pairs
        count = 0  
        count7 = 0 
        m = 0 
        GapMin = -1
        GapMax = 0  
        GapSum = 0
        If f = 5 Then Print '100_000
        For n = 3 To TenPower-1
            If isPrime(n) Then
                Have7 = False
                If isTetraprime(n+1*signo) Then
                    If isTetraprime(n+2*signo) Then
                        count += 1
                        If f = 5 Then
                            Print Using "#######"; n;
                            If count Mod 10 = 0 Then Print
                        End If
                        If Have7 Then count7 += 1
                        If m <> 0 Then
                            Gap = n - m
                            If Gap <= GapMin Then GapMin = Gap
                            If Gap > GapMax Then GapMax = Gap
                            GapSum += Gap
                        End If
                        m = n
                    End If
                End If
                n += 1
            End If
        Next n
        Print Using !"\nFound ##,### primes under ##,###,### whose preceding neighboring pair are tetraprimes"; count; TenPower
        Print Using "of which #,### have a neighboring pair, one of whose factors is 7."; count7
        Print Using !"\nMinimum gap between & primes : ##,###"; count; GapMin
        Print Using "Average gap between & primes : ##,###"; count; (GapSum / (count-1))
        Print Using "Maximum gap between & primes : ##,###"; count; GapMax
        signo = signo * -1
    Next g
    TenPower *= 10
Next f

Sleep

  

You may also check:How to resolve the algorithm Draw a cuboid step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Textonyms step by step in the Io programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Sidef programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the ARM Assembly programming language