How to resolve the algorithm Successive prime differences step by step in the Arturo programming language
How to resolve the algorithm Successive prime differences step by step in the Arturo 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 Arturo programming language
Source code in the arturo programming language
LIM: 1000000
findDiffs: function [r][
if r=[1] -> return [[2 3]]
i: 3
tupled: map 0..dec size r 'x -> fold slice r 0 x [a b][a+b]
diffs: new []
while [i < LIM][
if prime? i [
prset: map tupled 't -> i + t
if every? prset 'elem -> prime? elem [
'diffs ++ @[@[i] ++ prset]
]
]
i: i + 2
]
diffs: filter diffs 'dd [
some? range (first dd)+1 (last dd)-1 'x -> and? [prime? x][not? contains? dd x]
]
return diffs
]
loop [[2] [1] [2 2] [2 4] [4 2] [6 4 2]] 'rng [
print ["Differences of" join.with:", " to [:string] rng]
diffs: findDiffs rng
print ["\tFirst: " join.with:" " to [:string] first diffs]
print ["\tLast: " join.with:" " to [:string] last diffs]
print ["\tCount: " size diffs]
]
You may also check:How to resolve the algorithm Order disjoint list items step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Morfa programming language
You may also check:How to resolve the algorithm Chowla numbers step by step in the Arturo programming language
You may also check:How to resolve the algorithm HTTPS/Authenticated step by step in the LiveCode programming language