Given two integers A and B, the task is to count the number of pronic numbers that are present in the range [A, B].
Examples:
Input: A = 3, B = 20
Output: 3
Explanation: The pronic numbers from the range [3, 20] are 6, 12, 20Input: A = 5000, B = 990000000
Output: 31393
Naive Approach: Follow the given steps to solve the problem:
- Initialize the count of pronic numbers to 0.
- For every number in the range [A, B], check whether it is a pronic integer or not
- If found to be true, increment the count.
- Finally, print the count
Below is the implementation of the above approach:
C++14
// C++ program for // the above approach #include <bits/stdc++.h> using namespace std; Â
// Function to check if x // is a Pronic Number or not bool checkPronic( int x) { Â
    for ( int i = 0; i <= ( int )( sqrt (x));          i++) { Â
        // Check for Pronic Number by         // multiplying consecutive         // numbers         if (x == i * (i + 1)) {             return true ;         }     }     return false ; } Â
// Function to count pronic // numbers in the range [A, B] void countPronic( int A, int B) {     // Initialise count     int count = 0; Â
    // Iterate from A to B     for ( int i = A; i <= B; i++) { Â
        // If i is pronic         if (checkPronic(i)) { Â
            // Increment count             count++;         }     } Â
    // Print count     cout << count; } Â
// Driver Code int main() { Â Â Â Â int A = 3, B = 20; Â
    // Function call to count pronic     // numbers in the range [A, B]     countPronic(A, B); Â
    return 0; } |
Java
// Java program to implement // the above approach import java.util.*; Â
class GFG{ Â
// Function to check if x // is a Pronic Number or not static boolean checkPronic( int x) { Â
    for ( int i = 0 ; i <= ( int )(Math.sqrt(x));          i++) { Â
        // Check for Pronic Number by         // multiplying consecutive         // numbers         if ((x == i * (i + 1 )) != false ) {             return true ;         }     }     return false ; } Â
// Function to count pronic // numbers in the range [A, B] static void countPronic( int A, int B) {     // Initialise count     int count = 0 ; Â
    // Iterate from A to B     for ( int i = A; i <= B; i++) { Â
        // If i is pronic         if (checkPronic(i) != false ) { Â
            // Increment count             count++;         }     } Â
    // Print count     System.out.print(count); } Â
// Driver Code public static void main(String args[]) { Â Â Â Â int A = 3 , B = 20 ; Â
    // Function call to count pronic     // numbers in the range [A, B]     countPronic(A, B); } } Â
// This code is contributed by sanjoy_62. |
Python3
# Python3 program for the above approach import math Â
# Function to check if x # is a Pronic Number or not def checkPronic(x) : Â Â Â Â for i in range ( int (math.sqrt(x)) + 1 ): Â
        # Check for Pronic Number by         # multiplying consecutive         # numbers         if (x = = i * (i + 1 )) :             return True     return False    # Function to count pronic # numbers in the range [A, B] def countPronic(A, B) :          # Initialise count     count = 0 Â
    # Iterate from A to B     for i in range (A, B + 1 ): Â
        # If i is pronic         if (checkPronic(i) ! = 0 ) : Â
            # Increment count             count + = 1              # Print count     print (count) Â
# Driver Code A = 3 B = 20 Â
# Function call to count pronic # numbers in the range [A, B] countPronic(A, B) Â
# This code is contributed by susmitakundugoaldanga. |
C#
// C# program for the above approach using System; public class GFG { Â
// Function to check if x // is a Pronic Number or not static bool checkPronic( int x) { Â
    for ( int i = 0; i <= ( int )(Math.Sqrt(x));          i++)     { Â
        // Check for Pronic Number by         // multiplying consecutive         // numbers         if ((x == i * (i + 1)) != false )         {             return true ;         }     }     return false ; } Â
// Function to count pronic // numbers in the range [A, B] static void countPronic( int A, int B) {        // Initialise count     int count = 0; Â
    // Iterate from A to B     for ( int i = A; i <= B; i++)     { Â
        // If i is pronic         if (checkPronic(i) != false )         { Â
            // Increment count             count++;         }     } Â
    // Print count     Console.Write(count); } Â
Â
// Driver Code public static void Main(String[] args) { Â Â Â Â int A = 3, B = 20; Â
    // Function call to count pronic     // numbers in the range [A, B]     countPronic(A, B); } } Â
// This code is contributed by code_hunt. |
Javascript
<script> Â
// Javascript program for // the above approach Â
// Function to check if x // is a Pronic Number or not function checkPronic(x) { Â
    for (let i = 0; i <= parseInt(Math.sqrt(x));          i++) { Â
        // Check for Pronic Number by         // multiplying consecutive         // numbers         if (x == i * (i + 1)) {             return true ;         }     }     return false ; } Â
// Function to count pronic // numbers in the range [A, B] function countPronic(A, B) {     // Initialise count     let count = 0; Â
    // Iterate from A to B     for (let i = A; i <= B; i++) { Â
        // If i is pronic         if (checkPronic(i)) { Â
            // Increment count             count++;         }     } Â
    // Print count     document.write(count); } Â
// Driver Code let A = 3, B = 20; Â
// Function call to count pronic // numbers in the range [A, B] countPronic(A, B); Â
</script> |
Â
Â
3
Â
Time Complexity: O((B – A) * ?(B – A))
Auxiliary Space: O(1)
Efficient Approach: Follow the below steps to solve the problem:
- Define a function pronic(N) to find the count of pronic integers which are ? N.
- Calculate the square root of N, say X.
- If product of X and X – 1 is less than or equal to N, return N.
- Otherwise, return N – 1.
- Print pronic(B) – pronic(A – 1) to get the count of pronic integers in the range [A, B]
Below is the implementation of the above approach:
C++14
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; Â
// Function to count pronic // numbers in the range [A, B] int pronic( int num) { Â Â Â Â // Check upto sqrt N Â Â Â Â int N = ( int ) sqrt (num); Â
    // If product of consecutive     // numbers are less than equal to num     if (N * (N + 1) <= num) {         return N;     } Â
    // Return N - 1     return N - 1; } Â
// Function to count pronic // numbers in the range [A, B] int countPronic( int A, int B) {     // Subtract the count of pronic numbers     // which are <= (A - 1) from the count     // f pronic numbers which are <= B     return pronic(B) - pronic(A - 1); } Â
// Driver Code int main() { Â Â Â Â int A = 3; Â Â Â Â int B = 20; Â
    // Function call to count pronic     // numbers in the range [A, B]     cout << countPronic(A, B); Â
    return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { Â
// Function to count pronic // numbers in the range [A, B] static int pronic( int num) { Â Â Â Â Â Â Â // Check upto sqrt N Â Â Â Â int N = ( int )Math.sqrt(num); Â
    // If product of consecutive     // numbers are less than equal to num     if (N * (N + 1 ) <= num)     {         return N;     } Â
    // Return N - 1     return N - 1 ; } Â
// Function to count pronic // numbers in the range [A, B] static int countPronic( int A, int B) {        // Subtract the count of pronic numbers     // which are <= (A - 1) from the count     // f pronic numbers which are <= B     return pronic(B) - pronic(A - 1 ); } Â
// Driver Code public static void main(String[] args) { Â Â Â Â int A = 3 ; Â Â Â Â int B = 20 ; Â
    // Function call to count pronic     // numbers in the range [A, B]     System.out.print(countPronic(A, B)); Â
} } Â
// This code is contributed by shikhasingrajput |
Python3
# Python3 program for the above approach Â
# Function to count pronic # numbers in the range [A, B] def pronic(num) : Â
    # Check upto sqrt N     N = int (num * * ( 1 / 2 )); Â
    # If product of consecutive     # numbers are less than equal to num     if (N * (N + 1 ) < = num) :         return N; Â
    # Return N - 1     return N - 1 ; Â
# Function to count pronic # numbers in the range [A, B] def countPronic(A, B) :          # Subtract the count of pronic numbers     # which are <= (A - 1) from the count     # f pronic numbers which are <= B     return pronic(B) - pronic(A - 1 ); Â
# Driver Code if __name__ = = "__main__" : Â
    A = 3 ;     B = 20 ; Â
    # Function call to count pronic     # numbers in the range [A, B]     print (countPronic(A, B)); Â
    # This code is contributed by AnkThon |
C#
// C# program for the above approach using System; public class GFG { Â
// Function to count pronic // numbers in the range [A, B] static int pronic( int num) { Â Â Â Â Â Â Â // Check upto sqrt N Â Â Â Â int N = ( int )Math.Sqrt(num); Â
    // If product of consecutive     // numbers are less than equal to num     if (N * (N + 1) <= num)     {         return N;     } Â
    // Return N - 1     return N - 1; } Â
// Function to count pronic // numbers in the range [A, B] static int countPronic( int A, int B) {        // Subtract the count of pronic numbers     // which are <= (A - 1) from the count     // f pronic numbers which are <= B     return pronic(B) - pronic(A - 1); } Â
// Driver Code public static void Main(String[] args) { Â Â Â Â int A = 3; Â Â Â Â int B = 20; Â
    // Function call to count pronic     // numbers in the range [A, B]     Console.Write(countPronic(A, B)); } } Â
// This code is contributed by 29AjayKumar |
Javascript
<script> Â
// Javascript program for the above approach Â
// Function to count pronic // numbers in the range [A, B] function pronic(num) { Â
    // Check upto sqrt N     var N = parseInt(Math.sqrt(num)); Â
    // If product of consecutive     // numbers are less than equal to num     if (N * (N + 1) <= num) {         return N;     } Â
    // Return N - 1     return N - 1; } Â
// Function to count pronic // numbers in the range [A, B] function countPronic(A, B) { Â
    // Subtract the count of pronic numbers     // which are <= (A - 1) from the count     // f pronic numbers which are <= B     return pronic(B) - pronic(A - 1); } Â
// Driver Code var A = 3; var B = 20; Â
// Function call to count pronic // numbers in the range [A, B] document.write(countPronic(A, B)); Â
// This code is contributed by noob2000. </script> |
Â
Â
3
Â
Time Complexity: O(log2(B))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!