Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Check if a number is Quartan Prime or not in C++
The Quartan primes are prime numbers that can be represented as x^4 + y^4. Where x, y > 0. Some Quartan prime numbers are {2, 17, 97, ?}.
In this article, we will learn how to check whether a number is Quartan Prime or not in C++. Consider the following input and output scenarios to understand the concept better:
Scenario 1
<b>Input:</b> 17 <b>Output:</b> The number is Quartan Prime <b>Explanation</b> Here, we can represent the given number in the form of x^4 + y^4; (1)^4 + (2)^4 => 1 + 16 = 17.
Scenario 2
<b>Input:</b> 12 <b>Output:</b> The number is not Quartan Prime. <b>Explanation</b> The given number cannot be represented as x^4 + y^4.
Checking Quartan Prime Number
It has been observed that every quartan prime can be expressed in the form 16n + 1. Therefore, we will implement our code based on this observation to determine if a number is a quartan prime.
Here is the following approach we will follow to check for the quartan prime of a given number:
- Firstly, check whether the given number is prime or not.
- If the number is prime, then divide it by 16.
- If it returns the remainder 1, then it is a quartan prime number; else not.
C++ Program to Check Quartan Prime
Here is the following example code for this in C++.
#include <iostream>
using namespace std;
bool isPrime(int n){
for(int i = 2; i<= n/2; i++){
if(n % i == 0){
return false;
}
}
return true;
}
bool is_quartan_prime(int n) {
if(isPrime(n) && ((n % 16) == 1)){
return true;
}
return false;
}
int main() {
int num = 97;
if(is_quartan_prime(num)){
cout << "The number is Quartan Prime";
}else{
cout << "The number is not Quartan Prime";
}
}
Output
The number is Quartan Prime
Advertisements
