Scene 34 of
Dynamic Programming in
*Core Technical Interview Questions for Software Engineers*

By *Amin Ariana*
—
November 2013

You are given n, a positive integer. Return true if n is a prime number, and false if it's not.

##

Problem Statement

You are given n, a positive integer. Return true if n is a prime number, and false if it's not.

##

Evaluation

- Correct idea: 25%
- Answer with no bugs: 25%
- Square Root optimization: 25%
- Memoization suggestion: 25%

##

Solution

My one liner solution in Ruby without memoization:

```
def isPrime(n)
```

n > 1 and (2..Math.sqrt(n)).find{ |i| n % i == 0 }.nil?

end

Testing:

```
isPrime(1)
```

=> false

isPrime(2)

=> true

isPrime(3)

=> true

isPrime(4)

=> false

isPrime(3571)

=> true

##

References

Asked by Noah Kindler. Video http://vimeo.com/79718197 , his blog: http://www.codewod.com/ .