2

我正在尝试在 Java 中查找具有特定条件的素数。

挑战是显示所有包含“3”的素数(100.000 以下)四次。我已经有一个代码,它显示了 100.000 以下的所有质数,但我似乎无法弄清楚如何计算包含数字“3”的那些四次。然而,我可以数出所有的素数。

有人可以帮我弄这个吗?

这是我的代码,我将在哪里将数字放入字符串中?

    package Proeftentamen;

import java.util.regex.*;

/**
 *
 * @author Stefan
 */
public class Vraag_6 {

    /// priemgetallen waar 4x een 3 in voor komt???? wtf...
    public static void main(String[] args) {
        boolean[] lijst = new boolean[1000000]; // hoeveelheid getallen
        vularray(lijst);
        lijst = zeef(lijst);
        drukaf(lijst);
    }

    public static void vularray(boolean[] lijst) {
        for (int i = 2; i < lijst.length; i++) {
            lijst[i] = true;
        }
    }

    public static boolean[] zeef(boolean[] lijst) {
        for (int i = 2; i < lijst.length / 2; i++) {
            if (lijst[i]) {
                for (int j = 2 * i; j < lijst.length; j += i) {
                    lijst[j] = false;
                }
            }
        }
        return lijst;
    }

    public static void drukaf(boolean[] lijst) {
        int count = 0;
        for (int i = 2; i < lijst.length; i++) {
            if (lijst[i] == true) {
                System.out.println(i + " " + lijst[i]);
                count++;
            }
        }
        System.out.println("Aantal priemgetallen: " + count);
    }
}
4

6 回答 6

3

这个问题听起来真的像一个家庭作业,所以你应该写下你想出的东西和到目前为止你尝试过的东西。

数数的方法有很多。只是给你一个线索,你可以使用提醒操作(在Java - %):

56 % 10 = 6
25 % 5  = 0

So, when you divide by 10 and use a reminder operation you can get the last digit of your number. Now use a loop and counter and you'll be fine.

Another option (very ugly, so don't really use it :) ) - to turn your number into a String and iterate (loop) over its characters.

Hope this helps and good luck!

于 2012-07-03T13:32:22.690 回答
3

This code generate 50 permutation of numbers that has four '3' in it's digits so check each number that is prime or not

public void generateNumbers() {
    StringBuilder s = new StringBuilder();
    s.append("3333");
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= 9; j++) {
            if (j%3==0) continue;
            s.insert(i,String.valueOf(j));
            int number=Integer.parseInt(s.toString());
            System.out.println(number);
            s.delete(i,i+1);
        }
    }


}
于 2012-07-03T14:20:21.650 回答
1
  1. Iterate across each prime number.
  2. For each prime number, convert it to a string using the Integer.toString(int) static method.
  3. With this string, iterate over every character (use a for loop and the non-static method String.charAt(int index)) and count the number of times that method returns '3'. (The character '3', not the String "3").

Unless you have some other purpose for an array of prime-number Strings, don't bother to store them anywhere outside the loop.

于 2012-07-03T13:35:01.517 回答
0

Please refer below code to validate all such prime numbers.

void getPrimes(int num ,int frequency,char digit) {

    int count = 0;
    String number=Integer.toString(num);

    for (int i = 0; i < number.length(); i++) {
        if (count < frequency) {
            if (number.charAt(i) == digit)
                count++;
        }
        if (count == frequency)
        {
            System.out.println(number);
            return ;
        }

    }

}
于 2012-07-03T14:10:22.067 回答
0

Using the primes function from an exercise on the Sieve of Eratosthenes, as well as the digits and filter functions from the Standard Prelude, this Scheme expression finds the seven solutions:

(filter
  (lambda (n)
    (= (length
         (filter
           (lambda (d) (= d 3))
           (digits n)))
       4))
  (primes 100000))

The outer filter runs over all the primes less than 100000 and applies the test of the outer lambda to each. The inner filter computes the digits of each prime number and keeps only the 3s, then the length function counts them and the equality predicate keeps only those that have 4 3s. You can run the program and see the solution at http://codepad.org/e98fow2u.

于 2012-07-03T16:19:11.580 回答
0

you only have at most five digits, four of which must be 3. So what can you say about the remaining digit?

It's not hard to just write out the resulting numbers by hand, and then test each one for primality. Since there are no more than 50 numbers to test, even the simplest trial division by odds will do.

But if you want to generate the numbers programmatically, just do it with 5 loops: add 10,000 to 03333 9 times; add 1,000 to 30333 9 times; add 100 to 33033 9 times; etc. In C++:

int results[50];
int n_res = 0;
int a[5] = {13333, 31333, 33133, 33313, 33331};
for( int i=0, d=10000; i<5; ++i, d/=10)
  for( int j=1; j<9; ++j, a[i]+=d )
    if( is_prime(a[i]) )
      results[n_res++] = a[i];
于 2012-07-04T06:21:43.577 回答