Tetradic Primes
Last Updated :
23 Dec, 2023
A Tetradic Primes are prime numbers that are also tetradic number.
A tetradic Number is a palindromic number containing only 0, 1, and 8 as digits in the number.
Find the Tetradic prime numbers less than N
Given a number N, the task is to print all tetradic primes smaller than or equal to N.
Examples:
Input: N = 20
Output: 11
Input: N = 200
Output: 11 101 181
Approach: The idea is to generate all prime numbers smaller than or equal to the given number N and checking every prime number whether it is tetradic or not.
- To find if a given number is prime or not using sieve-of-eratosthenes method.
- To check whether the given number is tetradic number or not check that the number is a palindromic or not and only contains digits as 0, 1, and 8.
Below is the implementation of above algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
bool isContaindigit( int n)
{
while (n > 0)
{
if (!(n % 10 == 0 ||
n % 10 == 1 ||
n % 10 == 8))
return false ;
n = n / 10;
}
return true ;
}
bool ispalindrome( int n)
{
string temp = to_string(n);
int l = temp.length();
for ( int i = 0; i < l / 2; i++)
{
if (temp[i] != temp[l - i - 1])
return false ;
}
return true ;
}
bool isTetradic( int n)
{
if (ispalindrome(n) && isContaindigit(n))
return true ;
return false ;
}
void printTetradicPrimesLessThanN( int n)
{
bool prime[n + 1];
memset (prime, true , sizeof (prime));
int p = 2;
while (p * p <= n)
{
if (prime[p])
{
for ( int i = p * 2; i < n + 1; i += p)
prime[i] = false ;
}
p += 1;
}
for (p = 2; p < n + 1; p++)
{
if (prime[p] && isTetradic(p))
cout << p << " " ;
}
}
int main()
{
int n = 1000;
printTetradicPrimesLessThanN(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static boolean isContaindigit( int n)
{
while (n > 0 )
{
if (!(n % 10 == 0 ||
n % 10 == 1 ||
n % 10 == 8 ))
return false ;
n = n / 10 ;
}
return true ;
}
public static boolean ispalindrome( int n)
{
String temp = Integer.toString(n);
int l = temp.length();
for ( int i = 0 ; i < l / 2 ; i++)
{
if (temp.charAt(i) !=
temp.charAt(l - i - 1 ))
return false ;
}
return true ;
}
public static boolean isTetradic( int n)
{
if (ispalindrome(n) && isContaindigit(n))
return true ;
return false ;
}
public static void printTetradicPrimesLessThanN( int n)
{
boolean prime[] = new boolean [n + 1 ];
Arrays.fill(prime, true );
int p = 2 ;
while (p * p <= n)
{
if (prime[p])
{
for ( int i = p * 2 ; i < n + 1 ; i += p)
prime[i] = false ;
}
p += 1 ;
}
for (p = 2 ; p < n + 1 ; p++)
{
if (prime[p] && isTetradic(p))
System.out.print(p + " " );
}
}
public static void main(String[] args)
{
int n = 1000 ;
printTetradicPrimesLessThanN(n);
}
}
|
Python3
def isContaindigit(n):
temp = str (n)
for i in temp:
if i not in [ '0' , '1' , '8' ]:
return False
return True
def ispalindrome(n):
temp = str (n)
if temp = = temp[:: - 1 ]:
return True
return False
def isTetradic(n):
if ispalindrome(n):
if isContaindigit(n):
return True
return False
def printTetradicPrimesLessThanN(n):
prime = [ True ] * (n + 1 );
p = 2 ;
while (p * p < = n):
if (prime[p]):
for i in range (p * 2 , n + 1 , p):
prime[i] = False ;
p + = 1 ;
for p in range ( 2 , n + 1 ):
if (prime[p] and isTetradic(p)):
print (p, end = " " );
n = 1000 ;
printTetradicPrimesLessThanN(n);
|
C#
using System;
class GFG{
static bool isContaindigit( int n)
{
while (n > 0)
{
if (!(n % 10 == 0 ||
n % 10 == 1 ||
n % 10 == 8))
return false ;
n = n / 10;
}
return true ;
}
static bool ispalindrome( int n)
{
string temp = n.ToString();
int l = temp.Length;
for ( int i = 0; i < l / 2; i++)
{
if (temp[i] != temp[l - i - 1])
return false ;
}
return true ;
}
static bool isTetradic( int n)
{
if (ispalindrome(n) &&
isContaindigit(n))
return true ;
return false ;
}
static void printTetradicPrimesLessThanN( int n)
{
bool [] prime = new bool [n + 1];
Array.Fill(prime, true );
int p = 2;
while (p * p <= n)
{
if (prime[p])
{
for ( int i = p * 2; i < n + 1; i += p)
prime[i] = false ;
}
p += 1;
}
for (p = 2; p < n + 1; p++)
{
if (prime[p] && isTetradic(p))
Console.Write(p + " " );
}
}
static void Main()
{
int n = 1000;
printTetradicPrimesLessThanN(n);
}
}
|
Javascript
<script>
function isContaindigit(n)
{
while (n > 0)
{
if (!(n % 10 == 0 ||
n % 10 == 1 ||
n % 10 == 8))
return false ;
n = parseInt(n / 10, 10);
}
return true ;
}
function ispalindrome(n)
{
let temp = n.toString();
let l = temp.length;
for (let i = 0; i < parseInt(l / 2, 10); i++)
{
if (temp[i] != temp[l - i - 1])
return false ;
}
return true ;
}
function isTetradic(n)
{
if (ispalindrome(n) &&
isContaindigit(n))
return true ;
return false ;
}
function printTetradicPrimesLessThanN(n)
{
let prime = new Array(n + 1);
prime.fill( true );
let p = 2;
while (p * p <= n)
{
if (prime[p])
{
for (let i = p * 2; i < n + 1; i += p)
prime[i] = false ;
}
p += 1;
}
for (p = 2; p < n + 1; p++)
{
if (prime[p] && isTetradic(p))
document.write(p + " " );
}
}
let n = 1000;
printTetradicPrimesLessThanN(n);
</script>
|
Approach 2:
Another approach to find all tetradic primes less than or equal to N is to iterate through all odd numbers from 3 to N, and for each odd number check whether it is a prime and whether it is a tetradic number. To check whether a number is a prime, we can use a simple primality test such as trial division by odd numbers up to the square root of the number. To check whether a number is tetradic, we can convert it to a string and check whether all of its digits are 0, 1, or 8, and whether it is a palindrome.
Here are the codes for this approach:
C++
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
bool is_prime( int n) {
if (n < 2) {
return false ;
}
if (n == 2) {
return true ;
}
if (n % 2 == 0) {
return false ;
}
for ( int i = 3; i <= sqrt (n); i += 2) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
bool is_tetradic( int n) {
string s = to_string(n);
for ( char c : s) {
if (c != '0' && c != '1' && c != '8' ) {
return false ;
}
}
string rev_s = s;
reverse(rev_s.begin(), rev_s.end());
return s == rev_s;
}
void print_tetradic_primes( int n) {
for ( int i = 3; i <= n; i += 2) {
if (is_prime(i) && is_tetradic(i)) {
cout << i << " " ;
}
}
cout << endl;
}
int main() {
int n = 1000;
print_tetradic_primes(n);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class GFG {
private static boolean isPrime( int n) {
if (n < 2 ) {
return false ;
}
if (n == 2 ) {
return true ;
}
if (n % 2 == 0 ) {
return false ;
}
for ( int i = 3 ; i <= Math.sqrt(n); i += 2 ) {
if (n % i == 0 ) {
return false ;
}
}
return true ;
}
private static boolean isTetradic( int n) {
String s = Integer.toString(n);
for ( char c : s.toCharArray()) {
if (c != '0' && c != '1' && c != '8' ) {
return false ;
}
}
String rev_s = new StringBuilder(s).reverse().toString();
return s.equals(rev_s);
}
private static void printTetradicPrimes( int n) {
for ( int i = 3 ; i <= n; i += 2 ) {
if (isPrime(i) && isTetradic(i)) {
System.out.print(i + " " );
}
}
System.out.println();
}
public static void main(String[] args) {
int n = 1000 ;
printTetradicPrimes(n);
}
}
|
Python3
import math
def is_prime(n):
if n < 2 :
return False
if n = = 2 :
return True
if n % 2 = = 0 :
return False
for i in range ( 3 , int (math.sqrt(n)) + 1 , 2 ):
if n % i = = 0 :
return False
return True
def is_tetradic(n):
s = str (n)
for c in s:
if c not in [ '0' , '1' , '8' ]:
return False
rev_s = s[:: - 1 ]
return s = = rev_s
def print_tetradic_primes(n):
for i in range ( 3 , n + 1 , 2 ):
if is_prime(i) and is_tetradic(i):
print (i, end = ' ' )
print ()
n = 1000
print_tetradic_primes(n)
|
C#
using System;
class GFG {
public static bool IsPrime( int n) {
if (n < 2) {
return false ;
}
if (n == 2) {
return true ;
}
if (n % 2 == 0) {
return false ;
}
for ( int i = 3; i <= Math.Sqrt(n); i += 2) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
public static bool IsTetradic( int n) {
string s = n.ToString();
foreach ( char c in s) {
if (c != '0' && c != '1' && c != '8' ) {
return false ;
}
}
char [] arr = s.ToCharArray();
Array.Reverse(arr);
string rev_s = new string (arr);
return s == rev_s;
}
public static void PrintTetradicPrimes( int n) {
for ( int i = 3; i <= n; i += 2) {
if (IsPrime(i) && IsTetradic(i)) {
Console.Write(i + " " );
}
}
Console.WriteLine();
}
public static void Main( string [] args) {
int n = 1000;
PrintTetradicPrimes(n);
}
}
|
Javascript
function isPrime(n) {
if (n < 2) {
return false ;
}
if (n === 2) {
return true ;
}
if (n % 2 === 0) {
return false ;
}
for (let i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i === 0) {
return false ;
}
}
return true ;
}
function isTetradic(n) {
const s = n.toString();
for (const c of s) {
if (c !== '0' && c !== '1' && c !== '8' ) {
return false ;
}
}
const revS = s.split( '' ).reverse().join( '' );
return s === revS;
}
function printTetradicPrimes(limit) {
const result = [];
for (let i = 3; i <= limit; i += 2) {
if (isPrime(i) && isTetradic(i)) {
result.push(i);
}
}
console.log(result.join( ' ' ));
}
const n = 1000;
printTetradicPrimes(n);
|
Time complexity: O(N^(3/2)), because we iterate through all odd numbers up to N .
Space complexity: O(log(N)), because we need to store the string representation of the number to check whether it is tetradic.’
Approach: Constant Space:
In this Approach the boolean array prime[ ] has been removed. Instead, each number is checked for primality using a loop that iterates from 2 to the square root of the number. The space complexity remains constant because no additional data structures are used, and the calculations are performed using a fixed number of variables.
Here is the code of above approach:
C++
#include <iostream>
#include <cmath>
using namespace std;
bool isContaindigit( int n)
{
while (n > 0)
{
if (!(n % 10 == 0 || n % 10 == 1 || n % 10 == 8))
return false ;
n = n / 10;
}
return true ;
}
bool ispalindrome( int n)
{
int temp = n;
int reverse = 0;
while (temp > 0)
{
reverse = reverse * 10 + temp % 10;
temp = temp / 10;
}
return (n == reverse);
}
bool isTetradic( int n)
{
return (ispalindrome(n) && isContaindigit(n));
}
void printTetradicPrimesLessThanN( int n)
{
for ( int p = 2; p <= n; p++)
{
if (isTetradic(p))
{
bool isPrime = true ;
for ( int i = 2; i <= sqrt (p); i++)
{
if (p % i == 0)
{
isPrime = false ;
break ;
}
}
if (isPrime)
cout << p << " " ;
}
}
}
int main()
{
int n = 1000;
printTetradicPrimesLessThanN(n);
return 0;
}
|
Java
import java.util.Scanner;
public class TetradicPrimes {
private static boolean isContainDigit( int n)
{
while (n > 0 ) {
if (!(n % 10 == 0 || n % 10 == 1
|| n % 10 == 8 )) {
return false ;
}
n /= 10 ;
}
return true ;
}
private static boolean isPalindrome( int n)
{
int temp = n;
int reverse = 0 ;
while (temp > 0 ) {
reverse = reverse * 10 + temp % 10 ;
temp /= 10 ;
}
return n == reverse;
}
private static boolean isTetradic( int n)
{
return isPalindrome(n) && isContainDigit(n);
}
private static void printTetradicPrimesLessThanN( int n)
{
for ( int p = 2 ; p <= n; p++) {
if (isTetradic(p)) {
boolean isPrime = true ;
for ( int i = 2 ; i <= Math.sqrt(p); i++) {
if (p % i == 0 ) {
isPrime = false ;
break ;
}
}
if (isPrime) {
System.out.print(p + " " );
}
}
}
}
public static void main(String[] args)
{
int n = 1000 ;
printTetradicPrimesLessThanN(n);
}
}
|
Python3
def isContaindigit(n):
while n > 0 :
if not (n % 10 = = 0 or n % 10 = = 1 or n % 10 = = 8 ):
return False
n / / = 10
return True
def ispalindrome(n):
temp = n
reverse = 0
while temp > 0 :
reverse = reverse * 10 + temp % 10
temp / / = 10
return n = = reverse
def isTetradic(n):
return ispalindrome(n) and isContaindigit(n)
def printTetradicPrimesLessThanN(n):
for p in range ( 2 , n + 1 ):
if isTetradic(p):
isPrime = True
for i in range ( 2 , int (p * * 0.5 ) + 1 ):
if p % i = = 0 :
isPrime = False
break
if isPrime:
print (p, end = " " )
n = 1000
printTetradicPrimesLessThanN(n)
|
C#
using System;
class Program
{
static bool IsContainDigit( int n)
{
while (n > 0)
{
if (!(n % 10 == 0 || n % 10 == 1 || n % 10 == 8))
return false ;
n = n / 10;
}
return true ;
}
static bool IsPalindrome( int n)
{
int temp = n;
int reverse = 0;
while (temp > 0)
{
reverse = reverse * 10 + temp % 10;
temp = temp / 10;
}
return (n == reverse);
}
static bool IsTetradic( int n)
{
return (IsPalindrome(n) && IsContainDigit(n));
}
static void PrintTetradicPrimesLessThanN( int n)
{
for ( int p = 2; p <= n; p++)
{
if (IsTetradic(p))
{
bool isPrime = true ;
for ( int i = 2; i <= Math.Sqrt(p); i++)
{
if (p % i == 0)
{
isPrime = false ;
break ;
}
}
if (isPrime)
Console.Write(p + " " );
}
}
}
static void Main()
{
int n = 1000;
PrintTetradicPrimesLessThanN(n);
Console.ReadLine();
}
}
|
Javascript
function isContaindigit(n) {
while (n > 0) {
if (!(n % 10 === 0 || n % 10 === 1 || n % 10 === 8)) {
return false ;
}
n = Math.floor(n / 10);
}
return true ;
}
function ispalindrome(n) {
let temp = n;
let reverse = 0;
while (temp > 0) {
reverse = reverse * 10 + temp % 10;
temp = Math.floor(temp / 10);
}
return n === reverse;
}
function isTetradic(n) {
return ispalindrome(n) && isContaindigit(n);
}
function printTetradicPrimesLessThanN(n) {
for (let p = 2; p <= n; p++) {
if (isTetradic(p)) {
let isPrime = true ;
for (let i = 2; i <= Math.floor(Math.sqrt(p)); i++) {
if (p % i === 0) {
isPrime = false ;
break ;
}
}
if (isPrime) {
process.stdout.write(p + " " );
}
}
}
}
let n = 1000;
printTetradicPrimesLessThanN(n);
|
Time complexity: O(N^(3/2)), because we iterate through all odd numbers up to N .
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...