Given two numbers L and R which signifies a range [L, R], the task is to print all the prime adam integers in this range.
Note: A number which is both prime, as well as adam, is known as a prime adam number.
Examples:
Input: L = 5, R = 100
Output: 11 13 31
Explanation:
The three numbers 11, 13, 31 are prime. They are also adam numbers.
Input: L = 70, R = 50
Output: Invalid Input
Approach: The idea used in this problem is to first check whether a number is prime or not. If it is prime, then check whether it is an adam number of not:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int reverse( int a)
{
int rev = 0;
while (a != 0)
{
int r = a % 10;
rev = rev * 10 + r;
a = a / 10;
}
return (rev);
}
int prime( int a)
{
int k = 0;
for ( int i = 2; i < a; i++)
{
if (a % i == 0)
{
k = 1;
break ;
}
}
if (k == 1)
{
return (0);
}
else
{
return (1);
}
}
int adam( int a)
{
int r1 = reverse(a);
int s1 = a * a;
int s2 = r1 * r1;
int r2 = reverse(s2);
if (s1 == r2)
{
return (1);
}
else
{
return (0);
}
}
void find( int m, int n)
{
if (m > n)
{
cout << " INVALID INPUT " << endl;
}
else
{
int c = 0;
for ( int i = m; i <= n; i++)
{
int l = prime(i);
int k = adam(i);
if ((l == 1) && (k == 1))
{
cout << i << "\t" ;
}
}
}
}
int main()
{
int L = 5, R = 100;
find(L, R);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int reverse( int a)
{
int rev = 0 ;
while (a != 0 ) {
int r = a % 10 ;
rev = rev * 10 + r;
a = a / 10 ;
}
return (rev);
}
public static int prime( int a)
{
int k = 0 ;
for ( int i = 2 ; i < a; i++) {
if (a % i == 0 ) {
k = 1 ;
break ;
}
}
if (k == 1 ) {
return ( 0 );
}
else {
return ( 1 );
}
}
public static int adam( int a)
{
int r1 = reverse(a);
int s1 = a * a;
int s2 = r1 * r1;
int r2 = reverse(s2);
if (s1 == r2) {
return ( 1 );
}
else {
return ( 0 );
}
}
public static void find( int m, int n)
{
if (m > n) {
System.out.println( " INVALID INPUT " );
}
else {
int c = 0 ;
for ( int i = m; i <= n; i++) {
int l = prime(i);
int k = adam(i);
if ((l == 1 ) && (k == 1 )) {
System.out.print(i + "\t" );
}
}
}
}
public static void main(String[] args)
{
int L = 5 , R = 100 ;
find(L, R);
}
}
|
Python3
def reverse(a):
rev = 0 ;
while (a ! = 0 ):
r = a % 10 ;
rev = rev * 10 + r;
a = a / / 10 ;
return (rev);
def prime(a):
k = 0 ;
for i in range ( 2 , a):
if (a % i = = 0 ):
k = 1 ;
break ;
if (k = = 1 ):
return ( 0 );
else :
return ( 1 );
def adam(a):
r1 = reverse(a);
s1 = a * a;
s2 = r1 * r1;
r2 = reverse(s2);
if (s1 = = r2):
return ( 1 );
else :
return ( 0 );
def find(m, n):
if (m > n):
print ( "INVALID INPUT\n" );
else :
c = 0 ;
for i in range (m, n):
l = prime(i);
k = adam(i);
if ((l = = 1 ) and (k = = 1 )):
print (i, "\t" , end = " " );
L = 5 ; R = 100 ;
find(L, R);
|
C#
using System;
class GFG{
public static int reverse( int a)
{
int rev = 0;
while (a != 0)
{
int r = a % 10;
rev = rev * 10 + r;
a = a / 10;
}
return (rev);
}
public static int prime( int a)
{
int k = 0;
for ( int i = 2; i < a; i++)
{
if (a % i == 0)
{
k = 1;
break ;
}
}
if (k == 1)
{
return (0);
}
else
{
return (1);
}
}
public static int adam( int a)
{
int r1 = reverse(a);
int s1 = a * a;
int s2 = r1 * r1;
int r2 = reverse(s2);
if (s1 == r2)
{
return (1);
}
else
{
return (0);
}
}
public static void find( int m, int n)
{
if (m > n)
{
Console.WriteLine( "INVALID INPUT" );
}
else
{
for ( int i = m; i <= n; i++)
{
int l = prime(i);
int k = adam(i);
if ((l == 1) && (k == 1))
{
Console.Write(i + "\t" );
}
}
}
}
public static void Main(String[] args)
{
int L = 5, R = 100;
find(L, R);
}
}
|
Javascript
<script>
function reverse(a)
{
let rev = 0;
while (a != 0) {
let r = a % 10;
rev = rev * 10 + r;
a = parseInt(a / 10, 10);
}
return (rev);
}
function prime(a)
{
let k = 0;
for (let i = 2; i < a; i++) {
if (a % i == 0) {
k = 1;
break ;
}
}
if (k == 1) {
return (0);
}
else {
return (1);
}
}
function adam(a)
{
let r1 = reverse(a);
let s1 = a * a;
let s2 = r1 * r1;
let r2 = reverse(s2);
if (s1 == r2) {
return (1);
}
else {
return (0);
}
}
function find(m, n)
{
if (m > n) {
document.write( " INVALID INPUT " + "</br>" );
}
else {
let c = 0;
for (let i = m; i <= n; i++) {
let l = prime(i);
let k = adam(i);
if ((l == 1) && (k == 1)) {
document.write(i + " " );
}
}
}
}
let L = 5, R = 100;
find(L, R);
</script>
|
Output:
11 13 31
Time Complexity: O(N2), where N is the maximum number R.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!