Find Prime Adam integers in the given range [L, R]

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:Â
Â
- Iterate through the given range [L, R].
- For every number, check if the number is prime or not.
- If it is a prime, then check whether the number is an adam number or not.
- If a number is both prime and adam, then print the number.
Below is the implementation of the above approach:Â
Â
C++
// C++ program to find all prime// adam numbers in the given range#include <bits/stdc++.h>using namespace std;Â
int reverse(int a){Â Â Â Â int rev = 0;Â Â Â Â while (a != 0)Â Â Â Â {Â Â Â Â Â Â Â Â int r = a % 10;Â
        // Reversing a number by taking         // remainder at a time        rev = rev * 10 + r;         a = a / 10;    }    return (rev);}Â
// Function to check if a number// is a prime or notint prime(int a){Â Â Â Â int k = 0;Â
    // Iterating till the number    for(int i = 2; i < a; i++)    {                // Checking for factors       if (a % i == 0)       {           k = 1;           break;       }    }Â
    // Returning 1 if the there are     // no factors of the number other    // than 1 or itself    if (k == 1)     {        return (0);    }    else    {        return (1);    }}Â
// Function to check whether a number// is an adam number or notint adam(int a){         // Reversing given number    int r1 = reverse(a);Â
    // Squaring given number    int s1 = a * a;Â
    // Squaring reversed number    int s2 = r1 * r1;Â
    // Reversing the square of the    // reversed number    int r2 = reverse(s2);Â
    // Checking if the square of the     // number and the square of its     // reverse are equal or not    if (s1 == r2)     {        return (1);    }    else    {        return (0);    }}Â
// Function to find all the prime// adam numbers in the given rangevoid find(int m, int n){Â
    // If the first number is greater    // than the second number,    // print invalid    if (m > n)     {        cout << " INVALID INPUT " << endl;    }    else    {        int c = 0;Â
        // Iterating through all the         // numbers in the given range        for(int i = m; i <= n; i++)        {                        // Checking for prime number           int l = prime(i);           // Checking for Adam number           int k = adam(i);           if ((l == 1) && (k == 1))           {               cout << i << "\t";           }        }    }}Â
// Driver codeint main(){Â Â Â Â int L = 5, R = 100;Â Â Â Â Â Â Â Â Â find(L, R);Â Â Â Â return 0;}Â
// This code is contributed by Amit Katiyar |
Java
// Java program to find all prime// adam numbers in the given rangeimport java.io.*;Â
class GFG {Â
    public static int reverse(int a)    {        int rev = 0;        while (a != 0) {            int r = a % 10;Â
            // reversing a number by taking             // remainder at a time            rev = rev * 10 + r;             a = a / 10;        }        return (rev);    }Â
    // Function to check if a number    // is a prime or not    public static int prime(int a)    {        int k = 0;Â
        // Iterating till the number        for (int i = 2; i < a; i++) {Â
            // Checking for factors            if (a % i == 0) {                k = 1;                break;            }        }Â
        // Returning 1 if the there are no factors        // of the number other than 1 or itself        if (k == 1) {            return (0);        }        else {            return (1);        }    }Â
    // Function to check whether a number    // is an adam number or not    public static int adam(int a)    {        // Reversing given number        int r1 = reverse(a);Â
        // Squaring given number        int s1 = a * a;Â
        // Squaring reversed number        int s2 = r1 * r1;Â
        // Reversing the square of the        // reversed number        int r2 = reverse(s2);Â
        // Checking if the square of the number        // and the square of its reverse        // are equal or not        if (s1 == r2) {            return (1);        }        else {            return (0);        }    }    // Function to find all the prime    // adam numbers in the given range    public static void find(int m, int n)    {Â
        // If the first number is greater        // than the second number,        // print invalid        if (m > n) {            System.out.println(" INVALID INPUT ");        }        else {Â
            int c = 0;Â
            // Iterating through all the numbers            // in the given range            for (int i = m; i <= n; i++) {Â
                // Checking for prime number                int l = prime(i);Â
                // Checking for Adam number                int k = adam(i);                if ((l == 1) && (k == 1)) {                    System.out.print(i + "\t");                }            }        }    }Â
    // Driver code    public static void main(String[] args)    {Â
        int L = 5, R = 100;        find(L, R);    }} |
Python3
# Python3 program to find all prime# adam numbers in the given rangeÂ
def reverse(a):Â
    rev = 0;    while (a != 0):             r = a % 10;Â
        # Reversing a number by taking         # remainder at a time        rev = rev * 10 + r;         a = a // 10;         return(rev);Â
# Function to check if a number# is a prime or notdef prime(a):Â
    k = 0;Â
    # Iterating till the number    for i in range(2, a):Â
        # Checking for factors        if (a % i == 0):            k = 1;            break;Â
    # Returning 1 if the there are     # no factors of the number other    # than 1 or itself    if (k == 1):        return (0);    else:        return (1);Â
# Function to check whether a number# is an adam number or notdef adam(a):Â
    # Reversing given number    r1 = reverse(a);Â
    # Squaring given number    s1 = a * a;Â
    # Squaring reversed number    s2 = r1 * r1;Â
    # Reversing the square of the    # reversed number    r2 = reverse(s2);Â
    # Checking if the square of the     # number and the square of its     # reverse are equal or not    if (s1 == r2):         return (1);    else:        return (0);     # Function to find all the prime# adam numbers in the given rangedef find(m, n):Â
    # If the first number is greater    # than the second number,    # print invalid    if (m > n):         print("INVALID INPUT\n");    else:        c = 0;Â
    # Iterating through all the     # numbers in the given range    for i in range(m, n):Â
        # Checking for prime number        l = prime(i);Â
        # Checking for Adam number        k = adam(i);        if ((l == 1) and (k == 1)):            print(i, "\t", end = " ");             # Driver codeL = 5; R = 100;find(L, R);Â
# This code is contributed by Code_Mech |
C#
// C# program to find all prime// adam numbers in the given rangeusing System;Â
class GFG{Â
public static int reverse(int a){Â Â Â Â int rev = 0;Â Â Â Â while (a != 0)Â Â Â Â {Â Â Â Â Â Â Â Â int r = a % 10;Â
        // Reversing a number by taking         // remainder at a time        rev = rev * 10 + r;         a = a / 10;    }    return (rev);}Â
// Function to check if a number// is a prime or notpublic static int prime(int a){Â Â Â Â int k = 0;Â
    // Iterating till the number    for(int i = 2; i < a; i++)    {               // Checking for factors       if (a % i == 0)       {           k = 1;           break;       }    }         // Returning 1 if the there are no factors    // of the number other than 1 or itself    if (k == 1)    {        return (0);    }    else    {        return (1);    }}Â
// Function to check whether a number// is an adam number or notpublic static int adam(int a){         // Reversing given number    int r1 = reverse(a);Â
    // Squaring given number    int s1 = a * a;Â
    // Squaring reversed number    int s2 = r1 * r1;Â
    // Reversing the square of the    // reversed number    int r2 = reverse(s2);Â
    // Checking if the square of the     // number and the square of its     // reverse are equal or not    if (s1 == r2)    {        return (1);    }    else    {        return (0);    }}Â
// Function to find all the prime// adam numbers in the given rangepublic static void find(int m, int n){Â
    // If the first number is greater    // than the second number,    // print invalid    if (m > n)    {        Console.WriteLine("INVALID INPUT");    }    else    {                 // Iterating through all the numbers        // in the given range        for(int i = m; i <= n; i++)        {                       // Checking for prime number           int l = prime(i);                       // Checking for Adam number           int k = adam(i);           if ((l == 1) && (k == 1))           {               Console.Write(i + "\t");           }        }    }}Â
// Driver codepublic static void Main(String[] args){Â Â Â Â int L = 5, R = 100;Â Â Â Â find(L, R);}}Â
// This code is contributed by Rohit_ranjan |
Javascript
<script>Â
    // JavaScript program to find all prime    // adam numbers in the given range         function reverse(a)    {        let rev = 0;        while (a != 0) {            let r = a % 10;              // reversing a number by taking            // remainder at a time            rev = rev * 10 + r;            a = parseInt(a / 10, 10);        }        return (rev);    }      // Function to check if a number    // is a prime or not    function prime(a)    {        let k = 0;          // Iterating till the number        for (let i = 2; i < a; i++) {              // Checking for factors            if (a % i == 0) {                k = 1;                break;            }        }          // Returning 1 if the there are no factors        // of the number other than 1 or itself        if (k == 1) {            return (0);        }        else {            return (1);        }    }      // Function to check whether a number    // is an adam number or not    function adam(a)    {        // Reversing given number        let r1 = reverse(a);          // Squaring given number        let s1 = a * a;          // Squaring reversed number        let s2 = r1 * r1;          // Reversing the square of the        // reversed number        let r2 = reverse(s2);          // Checking if the square of the number        // and the square of its reverse        // are equal or not        if (s1 == r2) {            return (1);        }        else {            return (0);        }    }    // Function to find all the prime    // adam numbers in the given range    function find(m, n)    {          // If the first number is greater        // than the second number,        // print invalid        if (m > n) {            document.write(" INVALID INPUT " + "</br>");        }        else {              let c = 0;              // Iterating through all the numbers            // in the given range            for (let i = m; i <= n; i++) {                  // Checking for prime number                let l = prime(i);                  // Checking for Adam number                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
Â
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 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



