Count Pronic numbers from a given range
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, 20
Input: 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
#include <bits/stdc++.h>
using namespace std;
bool checkPronic( int x)
{
for ( int i = 0; i <= ( int )( sqrt (x));
i++) {
if (x == i * (i + 1)) {
return true ;
}
}
return false ;
}
void countPronic( int A, int B)
{
int count = 0;
for ( int i = A; i <= B; i++) {
if (checkPronic(i)) {
count++;
}
}
cout << count;
}
int main()
{
int A = 3, B = 20;
countPronic(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean checkPronic( int x)
{
for ( int i = 0 ; i <= ( int )(Math.sqrt(x));
i++) {
if ((x == i * (i + 1 )) != false ) {
return true ;
}
}
return false ;
}
static void countPronic( int A, int B)
{
int count = 0 ;
for ( int i = A; i <= B; i++) {
if (checkPronic(i) != false ) {
count++;
}
}
System.out.print(count);
}
public static void main(String args[])
{
int A = 3 , B = 20 ;
countPronic(A, B);
}
}
|
Python3
import math
def checkPronic(x) :
for i in range ( int (math.sqrt(x)) + 1 ):
if (x = = i * (i + 1 )) :
return True
return False
def countPronic(A, B) :
count = 0
for i in range (A, B + 1 ):
if (checkPronic(i) ! = 0 ) :
count + = 1
print (count)
A = 3
B = 20
countPronic(A, B)
|
C#
using System;
public class GFG
{
static bool checkPronic( int x)
{
for ( int i = 0; i <= ( int )(Math.Sqrt(x));
i++)
{
if ((x == i * (i + 1)) != false )
{
return true ;
}
}
return false ;
}
static void countPronic( int A, int B)
{
int count = 0;
for ( int i = A; i <= B; i++)
{
if (checkPronic(i) != false )
{
count++;
}
}
Console.Write(count);
}
public static void Main(String[] args)
{
int A = 3, B = 20;
countPronic(A, B);
}
}
|
Javascript
<script>
function checkPronic(x)
{
for (let i = 0; i <= parseInt(Math.sqrt(x));
i++) {
if (x == i * (i + 1)) {
return true ;
}
}
return false ;
}
function countPronic(A, B)
{
let count = 0;
for (let i = A; i <= B; i++) {
if (checkPronic(i)) {
count++;
}
}
document.write(count);
}
let A = 3, B = 20;
countPronic(A, B);
</script>
|
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
#include <bits/stdc++.h>
using namespace std;
int pronic( int num)
{
int N = ( int ) sqrt (num);
if (N * (N + 1) <= num) {
return N;
}
return N - 1;
}
int countPronic( int A, int B)
{
return pronic(B) - pronic(A - 1);
}
int main()
{
int A = 3;
int B = 20;
cout << countPronic(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int pronic( int num)
{
int N = ( int )Math.sqrt(num);
if (N * (N + 1 ) <= num)
{
return N;
}
return N - 1 ;
}
static int countPronic( int A, int B)
{
return pronic(B) - pronic(A - 1 );
}
public static void main(String[] args)
{
int A = 3 ;
int B = 20 ;
System.out.print(countPronic(A, B));
}
}
|
Python3
def pronic(num) :
N = int (num * * ( 1 / 2 ));
if (N * (N + 1 ) < = num) :
return N;
return N - 1 ;
def countPronic(A, B) :
return pronic(B) - pronic(A - 1 );
if __name__ = = "__main__" :
A = 3 ;
B = 20 ;
print (countPronic(A, B));
|
C#
using System;
public class GFG
{
static int pronic( int num)
{
int N = ( int )Math.Sqrt(num);
if (N * (N + 1) <= num)
{
return N;
}
return N - 1;
}
static int countPronic( int A, int B)
{
return pronic(B) - pronic(A - 1);
}
public static void Main(String[] args)
{
int A = 3;
int B = 20;
Console.Write(countPronic(A, B));
}
}
|
Javascript
<script>
function pronic(num)
{
var N = parseInt(Math.sqrt(num));
if (N * (N + 1) <= num) {
return N;
}
return N - 1;
}
function countPronic(A, B)
{
return pronic(B) - pronic(A - 1);
}
var A = 3;
var B = 20;
document.write(countPronic(A, B));
</script>
|
Time Complexity: O(log2(B))
Auxiliary Space: O(1)
Last Updated :
02 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...