Both start off with an array that gets filled upon each iteration wherein the number is checked for divisibility against the rest of the numbers in the range. The solution to the right is more concise.
I think B is the better solution due to its brevity and clarity.
While the two solutions are variations of the same elements (for instance, a loop, Math.sqrt(), modulo, and array insertion), solution B gets at the essence of the problem far more effectively by onelining useful constructs like .each() and conditional execution via or.
Solution A uses Sieve of Eratosthenes algorithm. It's not required to try every integers to test prime property.
B is done in the smarter way while A is more straight forward.
A divides the number by prime numbers, and B divides the number by all numbers from 2 to sqrt(i).floor. So, A should be faster.
Both start off with an array that gets filled upon each iteration wherein the number is checked for divisibility against the rest of the numbers in the range. The solution to the right is more concise.
I think B is the better solution due to its brevity and clarity.
While the two solutions are variations of the same elements (for instance, a loop,
Math.sqrt()
, modulo, and array insertion), solution B gets at the essence of the problem far more effectively by onelining useful constructs like.each()
and conditional execution viaor
.Neither. Use a library.
use
require 'prime'
. but i like "b" better.Solution b. Short and sweet.
a
Solution B is not sure if 10001st prime number is included