Gap in Primes
Description:
The prime numbers are not regularly spaced. For example from 2
to 3
the gap is 1
.
From 3
to 5
the gap is 2
. From 7
to 11
it is 4
.
Between 2 and 50 we have the following pairs of 2-gaps primes:
3-5, 5-7, 11-13, 17-19, 29-31, 41-43
A prime gap of length n is a run of n-1 consecutive composite numbers between two successive primes (see: http://mathworld.wolfram.com/PrimeGaps.html).
We will write a function gap with parameters:
g
(integer >= 2) which indicates the gap we are looking form
(integer > 2) which gives the start of the search (m inclusive)n
(integer >= m) which gives the end of the search (n inclusive)
In the example above gap(2, 3, 50)
will return [3, 5] or (3, 5) or {3, 5}
which is the first pair between 3 and 50 with a 2-gap.
So this function should return the first pair of two prime numbers spaced with a gap of g
between the limits m
, n
if these numbers exist otherwise `nil or null or None or Nothing (or ... depending on the language).
In such a case (no pair of prime numbers with a gap of `g`)
In C: return [0, 0]
In C++, Lua, COBOL: return `{0, 0}`.
In F#: return `[||]`.
In Kotlin, Dart and Prolog: return `[]`.
In Pascal: return Type TGap (0, 0).
Examples:
-
gap(2, 5, 7) --> [5, 7] or (5, 7) or {5, 7}
gap(2, 5, 5) --> nil. In C++ {0, 0}. In F# [||]. In Kotlin, Dart and Prolog return
[]`gap(4, 130, 200) --> [163, 167] or (163, 167) or {163, 167}
([193, 197] is also such a 4-gap primes between 130 and 200 but it's not the first pair)
gap(6,100,110) --> nil or {0, 0} or ...
: between 100 and 110 we have101, 103, 107, 109
but101-107
is not a 6-gap because there is103
in between and103-109
is not a 6-gap because there is107
in between.You can see more examples of return in Sample Tests.
Note for Go
For Go: nil slice is expected when there are no gap between m and n. Example: gap(11,30000,100000) --> nil
Ref
Similar Kata:
Stats:
Created | Oct 14, 2015 |
Published | Oct 14, 2015 |
Warriors Trained | 84589 |
Total Skips | 31593 |
Total Code Submissions | 120396 |
Total Times Completed | 21912 |
Ruby Completions | 805 |
Python Completions | 6756 |
JavaScript Completions | 4961 |
CoffeeScript Completions | 19 |
C# Completions | 1243 |
Java Completions | 3017 |
Haskell Completions | 243 |
Clojure Completions | 59 |
C++ Completions | 1376 |
PHP Completions | 540 |
Crystal Completions | 13 |
C Completions | 783 |
TypeScript Completions | 555 |
Rust Completions | 467 |
Swift Completions | 215 |
Go Completions | 584 |
Shell Completions | 36 |
F# Completions | 40 |
R Completions | 60 |
OCaml Completions | 17 |
Kotlin Completions | 237 |
Scala Completions | 148 |
Julia Completions | 49 |
PowerShell Completions | 28 |
Nim Completions | 15 |
Reason Completions | 5 |
Racket Completions | 12 |
Forth Completions | 13 |
Fortran Completions | 7 |
Dart Completions | 191 |
Prolog Completions | 8 |
Pascal Completions | 8 |
Lua Completions | 52 |
Perl Completions | 10 |
Elm Completions | 5 |
COBOL Completions | 7 |
D Completions | 5 |
Total Stars | 1650 |
% of votes with a positive feedback rating | 87% of 2576 |
Total "Very Satisfied" Votes | 2036 |
Total "Somewhat Satisfied" Votes | 426 |
Total "Not Satisfied" Votes | 114 |