How to resolve the algorithm Successive prime differences step by step in the XPL0 programming language
How to resolve the algorithm Successive prime differences step by step in the XPL0 programming language
Table of Contents
Problem Statement
The series of increasing prime numbers begins: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ... The task applies a filter to the series returning groups of successive primes, (s'primes), that differ from the next by a given value or values. Example 1: Specifying that the difference between s'primes be 2 leads to the groups: (Known as Twin primes or Prime pairs) Example 2: Specifying more than one difference between s'primes leads to groups of size one greater than the number of differences. Differences of 2, 4 leads to the groups: In the first group 7 is two more than 5 and 11 is four more than 7; as well as 5, 7, and 11 being successive primes. Differences are checked in the order of the values given, (differences of 4, 2 would give different groups entirely). Note: Generation of a list of primes is a secondary aspect of the task. Use of a built in function, well known library, or importing/use of prime generators from other Rosetta Code tasks is encouraged.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Successive prime differences 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, D;
[if N < 2 then return false;
if (N&1) = 0 then return N = 2;
if rem(N/3) = 0 then return N = 3;
D:= 5;
while D*D <= N do
[if rem(N/D) = 0 then return false;
D:= D+2;
if rem(N/D) = 0 then return false;
D:= D+4;
];
return true;
];
char Prime(1_000_000), S;
int Diff, Diffs, Count, N, I, First, Last;
int Str, Len;
[for N:= 0 to 1_000_000-1 do
Prime(N):= if IsPrime(N) then ^1 else ^0;
Diffs:= [[2, 0, 0], [1, 0, 0], [2, 2+2, 0], [2, 4+2, 0], [4, 2+4, 0], [6, 4+6, 2+4+6]];
Str:= [ "101 ", "11 ", "10101 ", "1010001 ", "1000101 ", "1000001000101 "];
Len:= [ 3, 2, 5, 7, 7, 13];
for Diff:= 0 to 6-1 do
[Count:= 0;
for N:= 0 to 1_000_000-1 -13 do
[S:= Str(Diff);
for I:= 0 to Len(Diff)-1 do
if S(I) # Prime(N+I) then I:= 100;
if I < 100 then \have match
[Count:= Count+1;
if Count = 1 then First:= N;
Last:= N;
];
];
Text(0, "First: "); IntOut(0, First);
for I:= 0 to 2 do
if Diffs(Diff,I) # 0 then
[ChOut(0, ^ ); IntOut(0, First+Diffs(Diff,I))];
Text(0, " Last: "); IntOut(0, Last);
for I:= 0 to 2 do
if Diffs(Diff,I) # 0 then
[ChOut(0, ^ ); IntOut(0, Last+Diffs(Diff,I))];
Text(0, " Groups: "); IntOut(0, Count); CrLf(0);
];
]
You may also check:How to resolve the algorithm Abstract type step by step in the Lua programming language
You may also check:How to resolve the algorithm Idiomatically determine all the characters that can be used for symbols step by step in the Raku programming language
You may also check:How to resolve the algorithm Sierpinski carpet step by step in the Brainf*** programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the REXX programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the Agena programming language