Check If A Number Is Quartan Prime Or Not In C++ - C++ Programming Tutorial
C++ Course / Graph Algorithms / Check If A Number Is Quartan Prime Or Not In C++

Check If A Number Is Quartan Prime Or Not In C++

BLUF: Mastering Check If A Number Is Quartan Prime Or Not In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Check If A Number Is Quartan Prime Or Not In C++

C++ is renowned for its efficiency. Learn how Check If A Number Is Quartan Prime Or Not In C++ enables low-level control and high-performance computing in the tutorial below.

In this tutorial, we will explore the process of verifying whether a given number qualifies as a Quartan Prime in C++. A Quartan Prime is a unique prime number identified by its characteristics within the base 4 numeral system. To be classified as a Quartan Prime, a number must retain its prime status even when converted into base-4 notation. This criterion mandates that both the original number and its base-4 equivalent are only divisible by 1 and 3. This condition arises from the fact that in base-4 representation, a base-10 number can solely utilize the digits 1 and 3.

In this tutorial, we are going to create a C++ application to calculate and verify Quartan Prime numbers. The program will include topics such as checking for prime numbers, converting numbers between different bases, and analyzing digits. Below are the steps involved in determining whether a number qualifies as a quartan prime in C++.

Step 1: Understanding the Components

  • Prime Checking: In order to perform a sieve, we require a function to check the prime or rather 'primeness' of a number. A prime number is a positive integer number greater than one and it has divided by itself and one only.
  • Base Conversion: We add the number as a decimal and then convert it to base 4 from base 10.
  • Digit Validation: Finally, after converting a number from number n to its base-4 form, it is checked whether all numbers are either 1 or 3.
  • Step 2: Implementing the Functions

Let's break down our implementation into smaller functions :

  • isPrime: It checks if a number is prime.
  • toBase4: Some integer to four-base representation of integers.
  • isQuartanPrime: It joins the above two functions to know if a number is Quartan Prime or not.
  • Example:

Example

//Program to check if a number is Quartan Prime or not in C++

#include <iostream>

#include <string>

bool isPrimeNum(int num) {

    if (num <= 1) return false;

    if (num <= 3) return true;

    if (num % 2 == 0 || num % 3 == 0) return false;

    

    for (int i = 5; i * i <= num; i += 6) {

        if (num % i == 0 || num % (i + 2) == 0) return false;

    }

    return true;

}

std::string toBase4(int num) {

    if (num == 0) return "0";

    std::string base4Val = "";

    

    while (num > 0) {

        base4Val = std::to_string(num % 4) + base4Val;

        num /= 4;

    }

    return base4Val;

}

bool isQuartanPrimeNum(int num) {

    // Check if n is the prime number

    if (!isPrimeNum(num)) return false;

    

    // Convert num to base-4

    std::string base4Val = toBase4(num);

    

    for (char digit : base4Val) {

        if (digit != '1' && digit != '3') {

            return false;

        }

    }

    

    return true;

}

int main() {

    int number;

    std::cout << "Enter a number: ";

    std::cin >> number;

    if (isQuartanPrimeNum(number)) {

        std::cout << number << " is a Quartan Prime." << std::endl;

    } else {

        std::cout << number << " is not a Quartan Prime." << std::endl;

    }

    return 0;

}

Output:

Output

Enter a number: 17

17 is not a Quartan Prime.

Explanation:

The provided C++ script is designed to determine if a number qualifies as a Quartan Prime by combining primality testing with a specific base 4 evaluation. Initially, the isPrimeNum function is responsible for assessing the primality of a number through an effective algorithm that eliminates non-prime numbers by dividing up to their square root. Subsequently, the toBase4 function is employed to convert the number into its base 4 representation. The isQuartanPrimeNum function integrates these functions, using the former to verify primality and the latter to ensure that the base 4 representation contains only the digits 1 and 3. When both conditions are satisfied, the number is classified as a Quartan Prime. Within the main function, user input triggers the execution of the isQuartanPrimeNum function, followed by a message displaying whether the input number meets the specified criteria.

Advantages:

Several advantages of the Quartan Prime are as follows:

  • Simplicity: The concept described here is clear and easy to follow, and it is an additional advantage for implementation.
  • Modularity: This code is arranged in functions (isPrime, toBase4, isQuartanPrime), which increases the readability of the code and makes it easier to debug.
  • Efficiency for Small Numbers: The prime-checking algorithm is equally efficient to use on numbers for small to moderately large integers because of the trail division up to the square root of the number in question.
  • Educational Value: It is good to use in the education process when explaining prime numbers and how to convert bases, and an example of using modularity in programming.
  • Flexibility: Flexibility is achieved because of modifications, such as including the possibility of changing the criteria for various types of primes or adjusting the base for conversion.
  • Disadvantages:

Several disadvantages of the Quartan Prime are as follows:

  • Performance with Large Numbers: Because of its O(n) characteristics, the prime-checking function may turn out to be slow for very large numbers.
  • Limited Input Handling: If there are any negative values to be input, zero or one, the program does not work well because they are all left unchanged.
  • No Error Handling: There are no checks against user input from users. For example, any value other than integers will lead to program failure.
  • Conclusion:

In summary, Quartan Prime encompasses fundamental arithmetic concepts, such as identifying prime numbers and converting bases. The C++ implementation employs dynamic programming to divide the process into distinct functions for prime number verification, base conversion, and digit search, enhancing code clarity and adaptability. While effective for small to moderately large integers, potential improvements include bolstered error handling, optimized performance for larger values, and enhanced input validation. Beyond showcasing logical and procedural proficiency in programming, this program serves as a valuable tool for integrating mathematical principles and devising algorithmic solutions.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience