Posted on by Kalkicode
Code Mathematics

Check if a number is full prime number

A full prime number is a prime number that remains prime even when its digits are successively removed from the right. In other words, a full prime is a prime number where all of its digits are themselves prime numbers (2, 3, 5, or 7).

Problem Statement

The problem is to determine whether a given positive integer is a full prime number or not.

Idea to Solve the Problem

To solve this problem, we can follow these steps:

  1. Check if the number is a prime number.
  2. If the number is prime, extract each digit from right to left and check if all digits are either 2, 3, 5, or 7.

If both conditions are met, then the number is a full prime; otherwise, it's not.

Pseudocode

function isPrime(n)
    if n <= 1
        return false
    if n == 2 or n == 3 or n == 5
        return true
    for i from 2 to square root of n
        if n % i == 0
            return false
    return true

function isFullPrime(num)
    if isPrime(num)
        n = num
        while n > 0
            digit = n % 10
            if digit != 2 and digit != 3 and digit != 5 and digit != 7
                return false
            n = n / 10
        return true
    return false

function main
    isFullPrime(2357) // Test case A
    isFullPrime(231)  // Test case B

Algorithm Explanation

  1. The isPrime function checks if a given number n is prime using trial division.
  2. The isFullPrime function checks if a number is a full prime using the following steps:
    • First, it calls the isPrime function to check if the number is a prime.
    • If the number is prime, it extracts each digit from right to left using modulo 10 and checks if each digit is 2, 3, 5, or 7.
    • If all digits satisfy the condition, the function returns true; otherwise, it returns false.

Code Solution

// C Program
// Check if a number is full prime number
#include <stdio.h>

int isPrime(int n)
{
	if (n <= 1)
	{
		return 0;
	}
	//Base case
	if (n == 2 || n == 3 || n == 5)
	{
		return 1;
	}
	// Check divisibility of a number
	for (int i = n / 2; i > 1; --i)
	{
		if (n % i == 0)
		{
			return 0;
		}
	}
	return 1;
}
void isFullPrime(int num)
{
	int result = 0;
	if (isPrime(num) == 1)
	{
		result = 1;
		int n = num;
		int digit = 0;
		while (n > 0 && result == 1)
		{
			digit = n % 10;
			if (digit == 2 || digit == 3 || 
                digit == 5 || digit == 7)
			{
				n = n / 10;
			}
			else
			{
				// When digit is not prime
				result = 0;
			}
		}
	}
	if (result == 1)
	{
		printf("\n %d is full prime number ", num);
	}
	else
	{
		printf("\n %d is not full prime number ", num);
	}
}
int main()
{
	// Test A
	// n = 2357
	isFullPrime(2357);
	// Test B
	// n = 231
	isFullPrime(231);
	return 0;
}

Output

 2357 is full prime number
 231 is not full prime number
/*
    Java Program
    Check if a number is full prime number
*/
public class PrimeNumber
{
	public boolean isPrime(int n)
	{
		if (n <= 1)
		{
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5)
		{
			return true;
		}
		// Check divisibility of a number
		for (int i = n / 2; i > 1; --i)
		{
			if (n % i == 0)
			{
				return false;
			}
		}
		return true;
	}
	public void isFullPrime(int num)
	{
		boolean result = false;
		if (isPrime(num))
		{
			result = true;
			int n = num;
			int digit = 0;
			while (n > 0 && result)
			{
				// Get last digit
				digit = n % 10;
				if (digit == 2 || digit == 3 || 
                    digit == 5 || digit == 7)
				{
					n = n / 10;
				}
				else
				{
					// When digit is not prime
					result = false;
				}
			}
		}
		if (result)
		{
			System.out.print("\n " + num + " is full prime number ");
		}
		else
		{
			System.out.print("\n " + num + " is not full prime number ");
		}
	}
	public static void main(String[] args)
	{
		PrimeNumber task = new PrimeNumber();
		// Test A
		// n = 2357
		task.isFullPrime(2357);
		// Test B
		// n = 231
		task.isFullPrime(231);
	}
}

Output

 2357 is full prime number
 231 is not full prime number
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Check if a number is full prime number
*/
class PrimeNumber
{
	public: bool isPrime(int n)
	{
		if (n <= 1)
		{
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5)
		{
			return true;
		}
		// Check divisibility of a number
		for (int i = n / 2; i > 1; --i)
		{
			if (n % i == 0)
			{
				return false;
			}
		}
		return true;
	}
	void isFullPrime(int num)
	{
		bool result = false;
		if (this->isPrime(num))
		{
			result = true;
			int n = num;
			int digit = 0;
			while (n > 0 && result)
			{
				// Get last digit
				digit = n % 10;
				if (digit == 2 || digit == 3 || 
                    digit == 5 || digit == 7)
				{
					n = n / 10;
				}
				else
				{
					// When digit is not prime
					result = false;
				}
			}
		}
		if (result)
		{
			cout << "\n " << num 
                 << " is full prime number ";
		}
		else
		{
			cout << "\n " << num 
                 << " is not full prime number ";
		}
	}
};
int main()
{
	PrimeNumber *task = new PrimeNumber();
	// Test A
	// n = 2357
	task->isFullPrime(2357);
	// Test B
	// n = 231
	task->isFullPrime(231);
	return 0;
}

Output

 2357 is full prime number
 231 is not full prime number
// Include namespace system
using System;
/*
    Csharp Program
    Check if a number is full prime number
*/
public class PrimeNumber
{
	public Boolean isPrime(int n)
	{
		if (n <= 1)
		{
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5)
		{
			return true;
		}
		// Check divisibility of a number
		for (int i = n / 2; i > 1; --i)
		{
			if (n % i == 0)
			{
				return false;
			}
		}
		return true;
	}
	public void isFullPrime(int num)
	{
		Boolean result = false;
		if (this.isPrime(num))
		{
			result = true;
			int n = num;
			int digit = 0;
			while (n > 0 && result)
			{
				// Get last digit
				digit = n % 10;
				if (digit == 2 || digit == 3 || 
                    digit == 5 || digit == 7)
				{
					n = n / 10;
				}
				else
				{
					// When digit is not prime
					result = false;
				}
			}
		}
		if (result)
		{
			Console.Write("\n " + num + " is full prime number ");
		}
		else
		{
			Console.Write("\n " + num + " is not full prime number ");
		}
	}
	public static void Main(String[] args)
	{
		PrimeNumber task = new PrimeNumber();
		// Test A
		// n = 2357
		task.isFullPrime(2357);
		// Test B
		// n = 231
		task.isFullPrime(231);
	}
}

Output

 2357 is full prime number
 231 is not full prime number
package main
import "fmt"
/*
    Go Program
    Check if a number is full prime number
*/

func isPrime(n int) bool {
	if n <= 1 {
		return false
	}
	//Base case
	if n == 2 || n == 3 || n == 5 {
		return true
	}
	// Check divisibility of a number
	for i := n / 2 ; i > 1 ; i-- {
		if n % i == 0 {
			return false
		}
	}
	return true
}
func isFullPrime(num int) {
	var result bool = false
	if isPrime(num) {
		result = true
		var n int = num
		var digit int = 0
		for (n > 0 && result) {
			// Get last digit
			digit = n % 10
			if digit == 2 || digit == 3 || 
			   digit == 5 || digit == 7 {
				n = n / 10
			} else {
				// When digit is not prime
				result = false
			}
		}
	}
	if result {
		fmt.Print("\n ", num, " is full prime number ")
	} else {
		fmt.Print("\n ", num, " is not full prime number ")
	}
}
func main() {
	
	// Test A
	// n = 2357
	isFullPrime(2357)
	// Test B
	// n = 231
	isFullPrime(231)
}

Output

 2357 is full prime number
 231 is not full prime number
<?php
/*
    Php Program
    Check if a number is full prime number
*/
class PrimeNumber
{
	public	function isPrime($n)
	{
		if ($n <= 1)
		{
			return false;
		}
		//Base case
		if ($n == 2 || $n == 3 || $n == 5)
		{
			return true;
		}
		// Check divisibility of a number
		for ($i = (int)($n / 2); $i > 1; --$i)
		{
			if ($n % $i == 0)
			{
				return false;
			}
		}
		return true;
	}
	public	function isFullPrime($num)
	{
		$result = false;
		if ($this->isPrime($num))
		{
			$result = true;
			$n = $num;
			$digit = 0;
			while ($n > 0 && $result)
			{
				// Get last digit
				$digit = $n % 10;
				if ($digit == 2 || $digit == 3 || 
                    $digit == 5 || $digit == 7)
				{
					$n = (int)($n / 10);
				}
				else
				{
					// When digit is not prime
					$result = false;
				}
			}
		}
		if ($result)
		{
			echo("\n ".$num.
				" is full prime number ");
		}
		else
		{
			echo("\n ".$num.
				" is not full prime number ");
		}
	}
}

function main()
{
	$task = new PrimeNumber();
	// Test A
	// n = 2357
	$task->isFullPrime(2357);
	// Test B
	// n = 231
	$task->isFullPrime(231);
}
main();

Output

 2357 is full prime number
 231 is not full prime number
/*
    Node JS Program
    Check if a number is full prime number
*/
class PrimeNumber
{
	isPrime(n)
	{
		if (n <= 1)
		{
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5)
		{
			return true;
		}
		// Check divisibility of a number
		for (var i = parseInt(n / 2); i > 1; --i)
		{
			if (n % i == 0)
			{
				return false;
			}
		}
		return true;
	}
	isFullPrime(num)
	{
		var result = false;
		if (this.isPrime(num))
		{
			result = true;
			var n = num;
			var digit = 0;
			while (n > 0 && result)
			{
				// Get last digit
				digit = n % 10;
				if (digit == 2 || digit == 3 || 
                    digit == 5 || digit == 7)
				{
					n = parseInt(n / 10);
				}
				else
				{
					// When digit is not prime
					result = false;
				}
			}
		}
		if (result)
		{
			process.stdout.write("\n " + num + " is full prime number ");
		}
		else
		{
			process.stdout.write("\n " + num + " is not full prime number ");
		}
	}
}

function main()
{
	var task = new PrimeNumber();
	// Test A
	// n = 2357
	task.isFullPrime(2357);
	// Test B
	// n = 231
	task.isFullPrime(231);
}
main();

Output

 2357 is full prime number
 231 is not full prime number
#    Python 3 Program
#    Check if a number is full prime number
class PrimeNumber :
	def isPrime(self, n) :
		if (n <= 1) :
			return False
		
		# Base case
		if (n == 2 or n == 3 or n == 5) :
			return True
		
		i = int(n / 2)
		#  Check divisibility of a number
		while (i > 1) :
			if (n % i == 0) :
				return False
			
			i -= 1
		
		return True
	
	def isFullPrime(self, num) :
		result = False
		if (self.isPrime(num)) :
			result = True
			n = num
			digit = 0
			while (n > 0 and result) :
				#  Get last digit
				digit = n % 10
				if (digit == 2 or digit == 3 or 
                    digit == 5 or digit == 7) :
					n = int(n / 10)
				else :
					#  When digit is not prime
					result = False
				
			
		
		if (result) :
			print("\n ", num ," is full prime number ", end = "")
		else :
			print("\n ", num ," is not full prime number ", end = "")
		
	

def main() :
	task = PrimeNumber()
	#  Test A
	#  n = 2357
	task.isFullPrime(2357)
	#  Test B
	#  n = 231
	task.isFullPrime(231)

if __name__ == "__main__": main()

Output

  2357  is full prime number
  231  is not full prime number
#    Ruby Program
#    Check if a number is full prime number
class PrimeNumber 
	def isPrime(n) 
		if (n <= 1) 
			return false
		end

		# Base case
		if (n == 2 || n == 3 || n == 5) 
			return true
		end

		i = n / 2
		#  Check divisibility of a number
		while (i > 1) 
			if (n % i == 0) 
				return false
			end

			i -= 1
		end

		return true
	end

	def isFullPrime(num) 
		result = false
		if (self.isPrime(num)) 
			result = true
			n = num
			digit = 0
			while (n > 0 && result) 
				#  Get last digit
				digit = n % 10
				if (digit == 2 || digit == 3 || 
                    digit == 5 || digit == 7) 
					n = n / 10
				else
 
					#  When digit is not prime
					result = false
				end

			end

		end

		if (result) 
			print("\n ", num ," is full prime number ")
		else
 
			print("\n ", num ," is not full prime number ")
		end

	end

end

def main() 
	task = PrimeNumber.new()
	#  Test A
	#  n = 2357
	task.isFullPrime(2357)
	#  Test B
	#  n = 231
	task.isFullPrime(231)
end

main()

Output

 2357 is full prime number 
 231 is not full prime number 
/*
    Scala Program
    Check if a number is full prime number
*/
class PrimeNumber()
{
	def isPrime(n: Int): Boolean = {
		if (n <= 1)
		{
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5)
		{
			return true;
		}
		var i: Int = n / 2;
		// Check divisibility of a number
		while (i > 1)
		{
			if (n % i == 0)
			{
				return false;
			}
			i -= 1;
		}
		return true;
	}
	def isFullPrime(num: Int): Unit = {
		var result: Boolean = false;
		if (isPrime(num))
		{
			result = true;
			var n: Int = num;
			var digit: Int = 0;
			while (n > 0 && result)
			{
				// Get last digit
				digit = n % 10;
				if (digit == 2 || digit == 3 || 
                    digit == 5 || digit == 7)
				{
					n = n / 10;
				}
				else
				{
					// When digit is not prime
					result = false;
				}
			}
		}
		if (result)
		{
			print("\n " + num + " is full prime number ");
		}
		else
		{
			print("\n " + num + " is not full prime number ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PrimeNumber = new PrimeNumber();
		// Test A
		// n = 2357
		task.isFullPrime(2357);
		// Test B
		// n = 231
		task.isFullPrime(231);
	}
}

Output

 2357 is full prime number
 231 is not full prime number
/*
    Swift 4 Program
    Check if a number is full prime number
*/
class PrimeNumber
{
	func isPrime(_ n: Int) -> Bool
	{
		if (n <= 1)
		{
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5)
		{
			return true;
		}
		var i: Int = n / 2;
		// Check divisibility of a number
		while (i > 1)
		{
			if (n % i == 0)
			{
				return false;
			}
			i -= 1;
		}
		return true;
	}
	func isFullPrime(_ num: Int)
	{
		var result: Bool = false;
		if (self.isPrime(num))
		{
			result = true;
			var n: Int = num;
			var digit: Int = 0;
			while (n > 0 && result)
			{
				// Get last digit
				digit = n % 10;
				if (digit == 2 || digit == 3 || 
                    digit == 5 || digit == 7)
				{
					n = n / 10;
				}
				else
				{
					// When digit is not prime
					result = false;
				}
			}
		}
		if (result)
		{
			print("\n ", num ," is full prime number ", terminator: "");
		}
		else
		{
			print("\n ", num ," is not full prime number ", terminator: "");
		}
	}
}
func main()
{
	let task: PrimeNumber = PrimeNumber();
	// Test A
	// n = 2357
	task.isFullPrime(2357);
	// Test B
	// n = 231
	task.isFullPrime(231);
}
main();

Output

  2357  is full prime number
  231  is not full prime number
/*
    Kotlin Program
    Check if a number is full prime number
*/
class PrimeNumber
{
	fun isPrime(n: Int): Boolean
	{
		if (n <= 1)
		{
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5)
		{
			return true;
		}
		var i: Int = n / 2;
		// Check divisibility of a number
		while (i > 1)
		{
			if (n % i == 0)
			{
				return false;
			}
			i -= 1;
		}
		return true;
	}
	fun isFullPrime(num: Int): Unit
	{
		var result: Boolean = false;
		if (this.isPrime(num))
		{
			result = true;
			var n: Int = num;
			var digit: Int ;
			while (n > 0 && result)
			{
				// Get last digit
				digit = n % 10;
				if (digit == 2 || digit == 3 || 
                    digit == 5 || digit == 7)
				{
					n = n / 10;
				}
				else
				{
					// When digit is not prime
					result = false;
				}
			}
		}
		if (result)
		{
			print("\n " + num + " is full prime number ");
		}
		else
		{
			print("\n " + num + " is not full prime number ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PrimeNumber = PrimeNumber();
	// Test A
	// n = 2357
	task.isFullPrime(2357);
	// Test B
	// n = 231
	task.isFullPrime(231);
}

Output

 2357 is full prime number
 231 is not full prime number

Resultant Output Explanation

Let's analyze the output of the provided code:

2357 is full prime number
231 is not full prime number

The output indicates that 2357 is a full prime number because it is a prime number and all of its digits are prime (2, 3, 5, and 7). On the other hand, 231 is not a full prime number because the digit 1 is not a prime digit.

Time Complexity

  • The isPrime function has a time complexity of O(sqrt(n)), where n is the given number.
  • The isFullPrime function also has a time complexity of O(log n) due to the digit extraction process.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment