public class Lab5{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Please enter N: ");
int n = input.nextInt();
System.out.println("The palindrome prime numbers less than " + n + " are: ");
for (int x=2; x<n; x++){
if(isPrime(x) && isPalindrome(x)){
System.out.print(x+", ");
}
}
}
public static boolean isPrime(int x){
for (int i=2; i<x; i++){
if (x%i ==0)
return false;
}return true;
}
public static int reverse(int x){
int ans=0;
while (x != 0){
ans = ans*10 + x%10;
x=x/10;
}return ans;
}
public static boolean isPalindrome(int x){
if (x%reverse(x) ==0)
return true;
else return false;
}
}
거대한 숫자를 N으로 입력 할 때마다 (즉, 1,000,000) 내 프로그램이 무한 루프로 이동합니다. 나는 10 시간 안에 마감 시간 전에 그것을 고칠 수 있도록 도움이 필요하다. 어떤 도움을 주시면 감사하겠습니다.프라임 프론드롬 프로그램이 무한 루프로 바뀝니다
프로그래밍 논리가 좋지 않습니다. '2에서 sqrt (x)'까지 루프를 실행하십시오! –
디버깅을 시도 했습니까? – xxbbcc
당신이 가진 모든 선택 (즉,'StringBuilder'와'reverse()','ListIterator' 등) 중에서 이것은 정확히 최적이 아닙니다. 정말로 이것을 사용하고 여기에 덤핑하는 대신 일어나는 일을 찾으려면 코드를 디버깅해야합니다. – Mena