5.2K
27 Likes

DSA Crash Course [Part 4] - Is Prime Approach

In this lesson, Alvin explores the strategy to solving the following interview problem:

Write a function, is_prime, that takes in a number as an argument. The function should return a boolean indicating whether or not the given number is prime.

A prime number is a number that is only divisible by two distinct numbers: 1 and itself.

For example, 7 is a prime because it is only divisible by 1 and 7. For example, 6 is not a prime because it is divisible by 1, 2, 3, and 6.

You can assume that the input number is a positive integer.

is_prime(2) # -> True
is_prime(3) # -> True
is_prime(4) # -> False
is_prime(5) # -> True
is_prime(6) # -> False
is_prime(7) # -> True
is_prime(8) # -> False
is_prime(2017) # -> True
is_prime(2048) # -> False

If you need additional support taking these DSA skills and actually applying them, take Alvin's complete data structures and algorithms course on Structy. You can try out the concepts yourself in their interactive code editor and learn advanced DSA patterns like stack exhaustive recursion.

Use this link to get 20% off the entire Structy DSA learning experience.

Follow Alvin on LinkedIn: https://www.linkedin.com/in/alvin-zablan-b73a92117/