简体   繁体   English

java中1到100之间的素数

[英]prime numbers between 1 and 100 in java

Guys I have been trying to figure out why this code is not showing the appropriate results for almost 5 hours!!!伙计们,我一直在试图弄清楚为什么这段代码在将近 5 个小时内没有显示适当的结果!!! It's should be a really easy task but it's driving me crazy.这应该是一项非常简单的任务,但它让我发疯。 I do have to get the project to work with the included code and not another similar code.我必须让项目使用包含的代码而不是另一个类似的代码。 The issue that I have is that is not showing the prime numbers in the bottom of my software.我遇到的问题是我的软件底部没有显示素数。 From my understanding this code should display the prime numbers 1-100 and have the all in lines of 5 numbers except for the last one.根据我的理解,这段代码应该显示 1-100 的质数,并且除了最后一个数字外,所有的数字都是 5 行。 Some guidance is greatly appreciated.非常感谢一些指导。

public class Prime numbers 1-100{
public static void main (String[] args) {

    int number = 100;

    // Assume the number is prime
    boolean isPrime = true; // Is the current number prime?

    // Test if number is prime
    for (int divisor = 2; divisor <= number / 2; divisor++) {
        if (number % divisor == 0) { // If true, number is not prime
            isPrime = false; // Set isPrime to false
            break; // Exit for loop
        }
        // print prime numbers
        if(isPrime)
            System.out.println("Prime numbers between 1 and " + number);
    }
}

} }

Your code should have nested loops and an array which stores found prime numbers to be shown at the end.您的代码应该具有嵌套循环和一个数组,该数组存储要在最后显示的素数。

To print them at the end you should first print your statement, then use a loop to print the array containing prime numbers found.要在最后打印它们,您应该首先打印您的语句,然后使用循环打印包含找到的素数的数组。

If you want to print all the prime numbers from 1 to 100 then you must iterate through all these numbers (1st loop) and for each of them iterate through all its possible divisors (nested loop):如果你想打印从 1 到 100 的所有质数,那么你必须遍历所有这些数字(第一个循环),并为它们中的每一个迭代所有可能的除数(嵌套循环):

int number = 100;

boolean isPrime = false;

System.out.println("2");
System.out.println("3");

for (int i = 5; i <= number; i++) {
    for (int divisor = 2; divisor <= Math.sqrt(i); divisor++) {
        isPrime = !(i % divisor == 0);
        if (!isPrime)
            break;
    }

    if (isPrime)
        System.out.println("" + i);
}

Note that for 2 and 3 the condition could not be applied so they are printed first.请注意,对于 2 和 3,无法应用条件,因此首先打印它们。
I used @selbie's optimization hint <= sqrt(number)我使用了@selbie 的优化提示<= sqrt(number)

public class Prime{
public static void main (String[] args) {
    boolean isPrime = true; 
    // iterate over all numbers from 1 to 100
    for (int number = 1; number <= 100; number++) {
        // We want to believe it's a prime
        isPrime = true;
        // 1 is not a prime
        if (number == 1) isPrime = false;
        // iterate over all numbers which are lower then 'number'
        for (int divisor = 2; divisor < number && isPrime; divisor++) {
            // if we can divide by it, it's not a prime
            if (number % divisor == 0) {
                isPrime = false;
            }
        }
        // print if it's a prime
        if (isPrime) System.out.println(number+", ");
    }
}}

I want to thank everyone advice and tips.我要感谢大家的建议和提示。 And motivation for pushing me.以及推动我的动力。 I was able to get the program to work.我能够让程序运行。 See below见下文

public class primes {
public static void main (String[] args) {
    final int NUMBER_OF_PRIMES = 26; // Number of primes to display
    final int NUMBER_OF_PRIMES_PER_LINE = 5; // Display 5 numbers per line
    int count = 0; // Count the number of prime numbers
    int number = 1; // A number to be tested for primeness

    System.out.println("The prime numbers between 1 and 100 are \n");

    // Repeatedly find prime numbers
    while (count < NUMBER_OF_PRIMES) {
        // Assume the number is prime
        boolean isPrime = true; // Is the current number prime?

        // Test whether number is prime
        for (int divisor = 2; divisor <= number / 2; divisor++) {
            if (number % divisor == 0) { // If true, number is not prime
                isPrime = false; // Set isPrime to false
                break; // Exit the for loop
            }
        }

        // Display the prime number and increase the count
        if (isPrime) {
            count++; // Increase the count

            if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
                // Display the number and advance to the new line
                System.out.println(number);
            }
            else
                System.out.print(number + " ");     
            }

        //Check if the next number is prime
        number++;
        }
    }
}

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

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