输入两个正整数A, B,求[A, B]内完全平方数的个数。( 1 ≤ A ≤ B ≤ 10^9 )
时间: 2024-06-14 16:03:59 浏览: 159
以下是一个求解[A, B]内完全平方数个数的Python程序示例:
```python
import math
def count_perfect_squares(A, B):
count = 0
for i in range(A, B+1):
if math.isqrt(i)**2 == i:
count += 1
return count
A = int(input("请输入A的值:"))
B = int(input("请输入B的值:"))
result = count_perfect_squares(A, B)
print("在[%d, %d]内的完全平方数个数为:%d" % (A, B, result))
```
相关问题
java输入两个正整数A, B,求[A, B]内完全平方数的个数。( 1 ≤ A ≤ B ≤ 10^9 )
以下是Java求[A, B]内完全平方数个数的示例代码:
```java
import java.util.Scanner;
public class CountPerfectSquares {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入正整数A:");
int A = scanner.nextInt();
System.out.print("请输入正整数B:");
int B = scanner.nextInt();
int count = 0;
for (int i = A; i <= B; i++) {
if (isPerfectSquare(i)) {
count++;
}
}
System.out.println("在[" + A + ", " + B + "]内的完全平方数个数为:" + count);
}
// 判断一个数是否为完全平方数
public static boolean isPerfectSquare(int num) {
int sqrt = (int) Math.sqrt(num);
return sqrt * sqrt == num;
}
}
```
Java输入两个正整数A, B,求[A, B]内完全平方数的个数。( 1 ≤ A ≤ B ≤ 10^9 )
以下是Java求[A, B]内完全平方数个数的示例代码:
```java
import java.util.Scanner;
public class CountPerfectSquares {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入正整数A:");
int A = scanner.nextInt();
System.out.print("请输入正整数B:");
int B = scanner.nextInt();
int count = 0;
for (int i = A; i <= B; i++) {
int sqrt = (int) Math.sqrt(i);
if (sqrt * sqrt == i) {
count++;
}
}
System.out.println("在[" + A + ", " + B + "]内的完全平方数个数为:" + count);
}
}
```
阅读全文