简体   繁体   English

嗨,我想打印用户输入的 2 个数字之间的所有回文素数

[英]Hi I want to print all the palindromic primes between 2 numbers the user inputs

So, I am done with first part and most of 2nd one but can not figure out how to do Palindromic primes, so please help.所以,我已经完成了第一部分和第二部分的大部分内容,但不知道如何做回文素数,所以请帮忙。 The code i am attaching is p1b and I have no idea what to do in p1c so please help.我附上的代码是 p1b,我不知道在 p1c 中做什么,所以请帮忙。

I. Introduction: The purpose of this project is to help you learn the basic JAVA programming language elements based upon your C/C++ background that you gained from CSC114.一、简介:本项目的目的是帮助你根据你在CSC114获得的C/C++背景,学习基本的JAVA编程语言元素。

II.二、 Assignment: Develop a JAVA program according to the following steps: Part A: Name this step p1a.java and you don't need to write a method other than main for this.作业:按照以下步骤开发一个JAVA程序: A部分:将此步骤命名为p1a.java,除main外无需编写其他方法。 1) The user is asked to enter an integer and your program is to produce the reverse of that number. 1) 要求用户输入一个整数,您的程序将生成该数字的倒数。 Example follows: Please enter an integer number: 2341 The reverse of 2341 is 1432示例如下: 请输入一个整数:2341 2341 的倒数是 1432

Part B: Name this step p1b.java. B 部分:将此步骤命名为 p1b.java。 1) Modify the last step using a method called reverse that returns the reverse value of a given integer. 1) 使用名为 reverse 的方法修改最后一步,该方法返回给定整数的反向值。 Then, modify the program so that it will runs as the following: 2) The user is asked to enter two integers, say x and y;然后,修改程序,使其运行如下: 2) 要求用户输入两个整数,例如 x 和 y; 3) So long as x is less than y, your program will do the following: a) Find and print all prime numbers between x and y. 3) 只要 x 小于 y,您的程序就会执行以下操作: a) 查找并打印 x 和 y 之间的所有质数。 (Need a method called isPrime that checks whether a given number is a prime or not.) b) Find and print all prime numbers that are also palindromes; (需要一个叫做 isPrime 的方法来检查给定的数是否是素数。) b) 查找并打印所有也是回文的素数; c) Ask for another pair of x and y. c) 要求另一对 x 和 y。 Example follows: Please enter two integer numbers: 5 400 Prime: 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 Palindromic Prime: 5 7 11 101 131 151 181 191 313 353 373 383 Please enter two integer numbers: 9 1 Done示例如下: 请输入两个整数: 5 400 质数: 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 1017 13 16 16 19 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397回文素数:5 7 11 101 131 151 181 191 313 353 373 383 请输入两个整数:9 1完成

Part C: Name this step p1c.java. C 部分:将此步骤命名为 p1c.java。 1) Traditionally, in order to produce the above output, the approach that one normally has is to go through the numbers twice: the first time is to find and print all prime numbers and then go through the numbers again to find and print all palindromic primes. 1) 传统上,为了产生上述输出,人们通常采用的方法是将数字遍历两次:第一次是查找并打印所有质数,然后再次遍历数字以查找并打印所有回文素数。 However, if we are able to keep the result and then print it when the process is completed, then it will only need 1 pass.但是,如果我们能够保留结果然后在过程完成时打印它,那么它只需要1遍。 2) Additionally, please display your output in a formatted way (such as aligned output in the previous page). 2)此外,请以格式化的方式显示您的输出(例如上一页中的对​​齐输出)。 3) Modify p1b.java so that it will only go through one pass to produce the same result as produced in part B. 3) 修改 p1b.java 使其只经过一次就产生与 B 部分相同的结果。

import java.util.Scanner;

public class p1b {

    public static void main(String[] args) {    
        System.out.println("Program for Finding Primem Numbers: ");
        System.out.println("==================================");   
        Scanner input = new Scanner(System.in);

        int num1, num2, i;
        int choice;
        System.out.print("Please enter the first number: ");
        num1 = input.nextInt();
        System.out.print("Please enter the Second number: ");
        num2 = input.nextInt();

        if (num1 > num2) {
            System.out.println("        Thank you.");
            System.exit(1); 
        }
        System.out.println( isPrime(num1, num2));
        System.out.println( reverse(num1));

        System.out.print("\nWould you like to enter two more numbers, "
                + "Enter 1 for Yes or 0 for No: ");
        choice = input.nextInt();
        if(choice == 1) {
            System.out.print("Please enter the first number: ");
            num1 = input.nextInt();
            System.out.print("Please enter the Second number: ");
            num2 = input.nextInt();
            System.out.println( isPrime(num1, num2));
        }

        if(choice == 0) {
            System.out.println("Bye!!");
            System.exit(0);
        }
    }

    public static int isPrime(int num1, int num2) {
        final int displayPerLine = 50;
        System.out.print("Prime: ");
        for ( int i = num1; i <= num2; i++ ) {
            int j;
            for ( j = 2; j < i; j++) {
                int number = i % j;
                if (number == 0) {
                    break;
                    //return n;
                }   
            }
            if(i == j) {
                System.out.printf("%-5d " + i);
            }
            if (i % displayPerLine == 0) {
                System.out.println();
            }
        }
        System.out.println();
            return num1;
    }

    //public static int reverse(int num1, int num2) {
    public static int reverse(int num1, int num2) {
        System.out.print("Palindrome: ");
        int palindrome = num1; // copied number into variable
        int reverse = palindrome;

        while (palindrome != 0) {
            int remainder = palindrome % 10;
            reverse = reverse * 10 + remainder;
            palindrome = palindrome / 10;
        }
        // if original and reverse of number is equal means
        // number is palindrome in Java
        if (num1 == reverse) {
            return reverse;
        }
        return palindrome;        
    }

    public static int reverse(int num) {
        int test = 0;           
        while (num != 0) {
            int lastdigit = num % 10;           
            test = test * 10 + lastdigit;
            num = num / 10;
        }
        return test;
    }       

    public static boolean isPalindrome(int num) {
        return num == reverse(num);
    }
}

Let's start by fixing isPrime , first test if the number is even;让我们从修复isPrime开始,首先测试数字是否为偶数; if so, it isn't prime.如果是这样,它不是素数。 Next, test odd numbers from 3 to the square root of the number.接下来,测试从 3 到数字的平方根的奇数。 If none of those are factors, then the number is prime.如果这些都不是因数,那么这个数就是素数。 Something like,就像是,

public static boolean isPrime(int i) {
    if (i % 2 == 0) {
        return false;
    }
    double sqrt = Math.sqrt(i);
    for (int t = 3; t <= sqrt; t += 2) {
        if (i % t == 0) {
            return false;
        }
    }
    return true;
}

Then we can implement isPalindrome with a StringBuilder like然后我们可以使用StringBuilder来实现isPalindrome ,例如

public static boolean isPalindrome(int i) {
    String str = String.valueOf(i);
    return new StringBuilder(str).reverse().toString().equals(str);
}

Finally, we can prompt for input in an infinite (here a while ) loop and use an inner for loop for printing primes that are also palindromes .最后,我们可以在无限(这里是while )循环中提示输入,并使用内部for循环来打印也是回文的 And, you can use Math.max and Math.min for getting the max and min respectively.而且,您可以使用Math.maxMath.min分别获取最大值和最小值。 Like,喜欢,

public static void main(String[] args) {
    System.out.println("\t\tProgram for Finding Prime Numbers: ");
    System.out.println("\t\t===================================");
    Scanner input = new Scanner(System.in);
    while (true) {
        System.out.print("Please enter the first number: ");
        int num1 = input.nextInt();
        System.out.print("Please enter the second number: ");
        int num2 = input.nextInt();
        int lo = Math.min(num1, num2);
        int hi = Math.max(num1, num2);
        for (int i = lo; i <= hi; i++) {
            if (isPalindrome(i) && isPrime(i)) {
                System.out.println(i);
            }
        }
        System.out.print("Would you like to enter two more numbers, "
                + "Enter 1 for Yes or 0 for No: ");
        int choice = input.nextInt();
        if (choice == 0) {
            break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM