What are Prime numbers ? Program to check if a number is Prime or not - A basic approach

Опубликовано: 14 Октябрь 2024
на канале: PrepBytes
2,595
49

Finding Prime Number is one of the early question we face in Competitive Programming.

We are bringing to your series of videos in which we discuss different kinds of questions you face around prime numbers and different approaches to solve them.

This is the first video in the prime number video series.

Here, we have discussed:

What are the prime numbers?
What is the most basic approach to determine whether a number is prime or not?
Code for the naive approach, that has time complexity O(N)

Let’s determine whether a number N is prime or not.

Naive Algorithm

Take an input N
For i = 2 to N-1 run a loop
For each ‘i’ check whether it divides N or not
If ‘i’ divide n
Break the loop and print not prime
Else
Continue
Print “prime” if the number is not divisible by any number between 2 and N-1.

Stay tuned for more prime number videos. We will publish the next video tomorrow.

Learn coding from experts through PrepBytes Personalised coding courses : http://bit.ly/2kLbko1