How to resolve the algorithm De Polignac numbers step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm De Polignac numbers step by step in the XPL0 programming language

Table of Contents

Problem Statement

Alphonse de Polignac, a French mathematician in the 1800s, conjectured that every positive odd integer could be formed from the sum of a power of 2 and a prime number. He was subsequently proved incorrect. The numbers that fail this condition are now known as de Polignac numbers. Technically 1 is a de Polignac number, as there is no prime and power of 2 that sum to 1. De Polignac was aware but thought that 1 was a special case. However. 127 is also fails that condition, as there is no prime and power of 2 that sum to 127. As it turns out, de Polignac numbers are not uncommon, in fact, there are an infinite number of them.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm De Polignac numbers step by step in the XPL0 programming language

Source code in the xpl0 programming language

func IsPrime(N);        \Return 'true' if N is prime
int  N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
    [if rem(N/I) = 0 then return false;
    I:= I+1;
    ];
return true;
];

func IsDePolignac(N);   \Return 'true' if N is a de Polignac number
int  N, P, T;
[for P:= N-1 downto 2 do
    if IsPrime(P) then
        [T:= 1;
        repeat  if T+P = N then return false;
                T:= T<<1;
        until   T+P > N;
        ];
return true;
];

int N, C;
[Format(5, 0);
N:= 1;  C:= 0;
loop    [if IsDePolignac(N) then
            [C:= C+1;
            if C<=50 or C=1000 or C=10000 then
                [RlOut(0, float(N));
                if rem(C/10) = 0 then CrLf(0);
                if C = 10000 then quit;
                ];
            ];
        N:= N+2;
        ];
]

define MAX_NUMBER = 500000;             \ maximum number we will consider     \
define MAX_POWER  =     20;             \ maximum powerOf2 < MAX_NUMBER       \
integer Prime      ( MAX_NUMBER+1 );
integer PowersOf2  ( MAX_POWER+1  );
integer DpCount;
integer RootMaxNumber;
integer I2, S, I;
integer P2, P;
integer Found;
begin \ find some De Polignac numbers - positive odd numbers that can't be    \
      \ written as p + 2^n for some prime p and integer n                     \
    begin
        \ Sieve the primes to MAX_NUMBER                                      \
        begin
            RootMaxNumber:= ( sqrt( MAX_NUMBER ) );
            Prime( 0 ) := false;
            Prime( 1 ) := false;
            Prime( 2 ) := true;
            for I := 3 to MAX_NUMBER do [Prime( I ) := true;   I:= I+1];
            for I := 4 to MAX_NUMBER do [Prime( I ) := false;  I:= I+1];
            for I := 3 to RootMaxNumber do begin
                if Prime( I ) then begin
                    I2 := I + I;
                    for S := I * I to MAX_NUMBER do [Prime( S ) := false;  S:= S+I2-1]
                end; \if_prime_i_and_i_lt_rootMaxNumber
                I:= I+1
            end \for_I
        end;
        \ Table of powers of 2 greater than 2^0 ( up to around 2 000 000 )    \
        \       Increase the table size if MAX_NUMBER > 2 000 000             \
        begin
            P2 := 1;
            for I := 1 to MAX_POWER do begin
                P2             := P2 * 2;
                PowersOf2( I ) := P2
            end \for_I
        end;
        \ The numbers must be odd and not of the form p + 2^n                 \
        \ Either p is odd and 2^n is even and hence n > 0 and p > 2           \
        \     or 2^n is odd and p is even and hence n = 0 and p = 2           \
        \ n = 0, p = 2 - the only possibility is 3                            \
        DpCount := 0;
        for I := 1 to 3 do begin
            P := 2;
            if P + 1 # I then begin
                DpCount := DpCount + 1;
                Format(5, 0);
                RlOut(0, float(I))
            end; \if_P_plus_1_ne_I
            I:= I+1
        end \for_I\ ;
        \ n > 0, p > 2                                                        \
        for I := 5 to MAX_NUMBER do begin
            Found := false;
            P := 1;
            while P <= MAX_POWER and not Found and I > PowersOf2( P ) do begin
                Found := Prime( I - PowersOf2( P ) );
                P     := P + 1
            end \while_not_found_and_have_a_suitable_power\ ;
            if not Found then begin
                DpCount := DpCount + 1;
                if DpCount <= 50 then begin
                    Format(5, 0);
                    RlOut(0, float(I));
                    if rem(DpCount / 10) = 0 then CrLf(0)
                    end
                else if DpCount = 1000 or DpCount = 10000 then begin
                    Format(5, 0);
                    Text(0, "The ");  RlOut(0, float(DpCount));
                    Format(6, 0);
                    Text(0, "th De Polignac number is ");  RlOut(0, float(I));
                    CrLf(0)
                end \if_various_DePolignac_numbers
            end; \if_not_found
        I:= I+1
        end \for_I\ ;
        Text(0, "Found ");  IntOut(0, DpCount);
        Text(0, " De Polignac numbers up to ");  IntOut(0, MAX_NUMBER);  CrLf(0)
    end
end

  

You may also check:How to resolve the algorithm SOAP step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Empty program step by step in the DCL programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the FALSE programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the Quackery programming language