hi guys.
i’m a little stuck, any tips?
Prime Number Finder: Prime Number Finder | Codecademy
The challenge isn’t placing any maximum value restriction on n
.
Here are a few things to consider:
Suppose the argument to the function is 6
, then the expected result should be [2, 3, 5]
whereas your function will return [2, 3, 5, 7]
. This contradicts the challenge specification “… and returns all the prime numbers from 1
to n
(inclusive)”. Your function doesn’t work properly when the provided argument is in the range 1 to 6 as your function is returning extra prime numbers.
You are deciding whether a number is prime or not by checking its divisibility by the numbers 2
, 3
, 5
and 7
. That approach will work when the provided argument is about 100
. But for numbers greater than 100
, you can’t just rely on divisibility by 2
, 3
, 5
and 7
.
For Example, if the argument to the function is 121
, then 121
is included in your list of primes
. But 121
is most certainly not a prime number (11 x 11 = 121
) because the factors of 121
are 1, 11, 121
. You could tack on
and num % 11 != 0
to your if condition. But, what happens if the provided argument is 169
? Your function would identify 169
as a prime number even though it is not (since 13 x 13 = 169
).
You can’t write an if condition with a finite number of possibilities beforehand which exhaustively guarantees that your function will work for all n
.
Your if condition will work up to a certain point and then it won’t. Manually tacking on more operands after the and
is not going to solve the underlying issue. You need to re-think your approach.
that’s a really interesting perspective , but how to fix it?