How to resolve the algorithm Twin primes step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Twin primes step by step in the ALGOL 68 programming language

Table of Contents

Problem Statement

Twin primes are pairs of natural numbers   (P1  and  P2)   that satisfy the following:

Write a program that displays the number of pairs of twin primes that can be found under a user-specified number (P1 < user-specified number & P2 < user-specified number).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Twin primes step by step in the ALGOL 68 programming language

Source code in the algol programming language

BEGIN
    # count twin primes (where p and p - 2 are prime)                             #
    PR heap=128M PR # set heap memory size for Algol 68G                          #
    # sieve of Eratosthenes: sets s[i] to TRUE if i is a prime, FALSE otherwise   #
    PROC sieve = ( REF[]BOOL s )VOID:
         BEGIN
            FOR i TO UPB s DO s[ i ] := TRUE OD;
            s[ 1 ] := FALSE;
            FOR i FROM 2 TO ENTIER sqrt( UPB s ) DO
                IF s[ i ] THEN FOR p FROM i * i BY i TO UPB s DO s[ p ] := FALSE OD FI
            OD
         END # sieve # ;
    # find the maximum number to search for twin primes                           #
    INT max;
    print( ( "Maximum: " ) );
    read( ( max, newline ) );
    INT max number = max;
    # construct a sieve of primes up to the maximum number                        #
    [ 1 : max number ]BOOL primes;
    sieve( primes );
    # count the twin primes                                                       #
    # note 2 cannot be one of the primes in a twin prime pair, so we start at 3   #
    INT twin count := 0;
    FOR p FROM 3 BY 2 TO max number - 1 DO IF primes[ p ] AND primes[ p - 2 ] THEN twin count +:= 1 FI OD;
    print( ( "twin prime pairs below  ", whole( max number, 0 ), ": ", whole( twin count, 0 ), newline ) )
END

  

You may also check:How to resolve the algorithm Fibonacci word/fractal step by step in the Processing programming language
You may also check:How to resolve the algorithm Unicode strings step by step in the Perl programming language
You may also check:How to resolve the algorithm EKG sequence convergence step by step in the 11l programming language
You may also check:How to resolve the algorithm Bioinformatics/Global alignment step by step in the Phix programming language
You may also check:How to resolve the algorithm CUSIP step by step in the Action! programming language