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

Input: 17
Output: The number is Quartan Prime
Explanation
Here, we can represent the given number in the form of x^4 + y^4;
(1)^4 + (2)^4 => 1 + 16 = 17.

Scenario 2

Input: 12
Output: The number is not Quartan Prime.
Explanation
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:

  1. Firstly, check whether the given number is prime or not.
  2. If the number is prime, then divide it by 16.
  3. 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
Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-07-23T18:48:18+05:30

378 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements