chore(CPlusPlus): created prime-check (#295)

* Created prime-check.cpp

Added new file in Maths section(prime-check.cpp).

* Updated Time complexity and addded test case.

Fixes indentation and minor typos.

* Fixed Indentation

* Fixed Indentation

* Updated Maths Section

* Fixed Typo
pull/307/head
Akash Negi 2021-05-15 16:47:05 +05:30 committed by GitHub
parent 0e62ebed99
commit 33406913d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,29 @@
#include<bits/stdc++.h>
using namespace std;
//Function to check whether a number is prime or not.
bool isPrime(int num) {
if (num == 1)
return false;
if (num == 2 || num == 3)
return true;
if (num % 2 == 0 || num % 3 == 0)
return false; //Multiples of prime numbers are not prime
for (int i = 6; i * i <= num; i += 5) {
//If number is a composite number then it must have one multiple greater(or equal)
//to sqrt(num) and one multiple less(or equal) to sqrt(num).
//If the num doesn't satisfy the above conditions then the number is said to be a prime number
//Also prime number appear in the form of 6n-1 and 6n+1 where n is a natural number
if ((num % (i - 1) == 0) || (num % (i + 1) == 0))
return false;
}
return true;
}
int main() {
int num;
cin >> num; //Taking input from user
string result = isPrime(num) ? "Prime" : "Not Prime"; //Evaluating string based on result from isPrime function
cout << result; //Printing result
//Ex : num = 11 , result will print "Prime"
//Time Complexity :O(sqrt(num))
return 0;
}

View File

@ -64,3 +64,4 @@
# Maths
1. [Kaprekar Number](Maths/Kaprekar-number.cpp)
2. [Prime Number](Maths/prime-check.cpp)