Answers for "find if a number is prime"

6

determine if number is prime python

# Time Efficient Primality Check in Python

def primeCheck(n):
    # 0, 1, even numbers greater than 2 are NOT PRIME
    if n==1 or n==0 or (n % 2 == 0 and n > 2):
        return "Not prime"
    else:
        # Not prime if divisable by another number less
        # or equal to the square root of itself.
        # n**(1/2) returns square root of n
        for i in range(3, int(n**(1/2))+1, 2):
            if n%i == 0:
                return "Not prime"
        return "Prime"
Posted by: Guest on September-17-2020
1

find is number prime

int number;
cin >> number;
if (number == 2) {
	cout << "Prime" << endl;
	return;
}else if(number == 1 || number % 2 == 0){
	cout << "Not prime" << endl;
	return;
}
for (int i = 3; i * i <= number; i+=2) {
	if(number % j == 0){
		cout << "Not prime" << endl;
		return;
	}
}
cout << "Prime" << endl;
return;
Posted by: Guest on June-17-2021
0

program to know if a number is prime

#include<bits/stdc++.h>
using namespace std;
bool Is_Prime(long long x){
	if(x%2==0)return false;
	for(int i=3;i*i<=x;i+=2)
		if(x%i==0)return false;
	return true;
}
int main(){
	long long x;
	cin>>x;
	if(Is_Prime(x))cout<<"Is Prime";
	else cout<<"Is not Prime";
}
Posted by: Guest on April-30-2020

Code answers related to "find if a number is prime"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language