Wednesday, 6 January 2016

Kata Day 41: Surrounding Primes for a value


Problem Description: We need a function prime_bef_aft() that gives the largest prime below a certain given value n, befPrime or bef_prime (depending on the language),and the smallest prime larger than this value,
aftPrime/aft_prime (depending on the language).

The result should be output in a list like the following:

primeBefAft == [befPrime, aftPrime]
If n is a prime number it will give two primes, n will not be included in the result.

Let's see some cases:

primeBefAft(100) == [97, 101]

primeBefAft(97) == [89, 101]

primeBefAft(101) == [97, 103]
Happy coding!!

Level:6kyu

Link To Kata: Surrounding Primes for a value

In this Kata the problem asks us to find the greatest prime number less than a given prime number and smallest prime number greater than the given prime number. 

We create an empty answer array. We loop in a decreasing order from a number one less than the given number. We push the number which is prime in the answer array and break out of the loop.

We do the same but in increasing order from the given number. We check for prime by a simple prime check function. In the end we output the answer array as the solution to this Kata.

Please go through the code for a better understanding. 

Here is a link to the code: Kata Day 41

Please mention your suggestions or your doubts in the comment below.

Happy coding. :)

No comments:

Post a Comment