[英]How to optimize the solution to calculate Pronic number in Java
Recently, in one of the assessments, I came across this question: Given two integers A and B, return the number of integers from the range A..B which can be expressed as the product of two consecutive integers, that is X *(X + 1) which is also known as Pronic Number.最近,在一次评估中,我遇到了这个问题:给定两个整数 A 和 B,返回范围 A..B 中的整数个数,可以表示为两个连续整数的乘积,即 X *( X + 1) 也称为普罗尼克数。
Example 1:示例 1:
A = 6 and B = 20, the function should return 3, These integers are 6 = 2 * 3, 12 = 3 * 4 and 20 = 4* 5 A = 6 和 B = 20,function 应该返回 3,这些整数是 6 = 2 * 3、12 = 3 * 4 和 20 = 4* 5
Example 2:示例 2:
A = 21 and B = 29, the function should return 0 A = 21 和 B = 29,function 应返回 0
Assumptions:假设:
My solution fails for the extreme input which is 1000,000,000.对于 1000,000,000 的极端输入,我的解决方案失败了。 I got Time Limit Exceeded error.我收到超过时间限制错误。 Can somebody please help me to optimize my code further?有人可以帮我进一步优化我的代码吗?
// Java program to check if a number is pronic or not
import java.io.*;
import java.util.*;
import java.math.*;
class solution
{
// Function to check Pronic Number
static int pronic_check(int A, int B)
{
int count = 0;
for (int i = A; i <= B; i++){
if (i % 2 == 0) // a pronic number is always even
{
int x = (int)(Math.sqrt(i));
if (x * (x + 1) == i)
count++;
}
}
return count;
}
public static void main(String[] args)
{
System.out.println(pronic_check(5000, 990000000));
}
}
Thanks in advance.提前致谢。
If you just need the count.如果你只需要计数。 I guess this one is going to be best for you.我想这个对你来说是最好的。
Explanation: Lets assume, A=6 B=20
.说明:假设, A=6 B=20
。 Now, start=sqrt(A)= 2
and end = sqrt(20) = 4
.现在, start=sqrt(A)= 2
和end = sqrt(20) = 4
。 Initially count = (end-start-1)
.最初count = (end-start-1)
。 Here, you need to check for only start
and end
.在这里,您只需要检查start
和end
。 If (end*(end+1))<= B
then just increase the count
by one.如果(end*(end+1))<= B
则只需将count
增加一。 Also check if (start*(start+1) >= A)
then increase count
by one.还要检查是否(start*(start+1) >= A)
然后将count
加一。 So, here the count = 3
.所以,这里的count = 3
。
And here the Time complexity will be constant O(1)
.这里的时间复杂度将是常数O(1)
。
import java.io.*;
import java.util.*;
import java.lang.Math;
class solution
{
// Function to check Pronic Number
static int pronic_check(int A, int B)
{
int count = 0;
int start = (int) Math.sqrt(A);
int end = (int) Math.sqrt(B);
count = (end -start -1 );
if (start*(start+1) >= A) {
count++;
}
if (end*(end+1) <= B){
count++;
}
return count;
}
public static void main(String[] args)
{
System.out.println(pronic_check(5000, 990000000));
}
}
@H.R. @H.R。 Emon Thanks for the intuitive solution! Emon 感谢您提供直观的解决方案!
Based on your idea, I tried modifying the solution a bit.根据您的想法,我尝试稍微修改解决方案。 We can find the count of (A-1)'s Pronic Number, a_pronic_count, and the count of B's Pronic Number, b_pronic_count, and use b_pronic_count - a_pronic_count to get the final answer.我们可以求出(A-1)的Pronic Number的计数a_pronic_count和B的Pronic Number的计数b_pronic_count,并使用b_pronic_count - a_pronic_count得到最终答案。 Time complexity is O(1).时间复杂度为 O(1)。
class solution
{
public static int pronic_count(int A, int B){
int a_cnt = count(A);
int b_cnt = count(B);
return b_cnt - a_cnt + 1;
}
public static int count(int num){
int n = (int)Math.sqrt(num);
if (n*(n+1) == num) return n;
return n-1;
}
public static void main(String[] args)
{
System.out.println(pronic_count(1, 1000000000));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.