Problem:
Charles Babbage (1792–1871) obtained the first government grant in history when in 1823
he persuaded the British government to provide £1000 to build his difference engine. In his
grant proposal, Babbage gave the formula x2 + x + 41 as an example of a function that his
computer would tabulate. This particular function was of interest to mathematicians because
it produces an unusual number of prime numbers.Primes that have this form n = x2 + x + 41
for some integer x could be called Babbage primes. Write a program that finds all the Babbage
primes that are less than 10,000. Your first
five lines of output should look like this:
he persuaded the British government to provide £1000 to build his difference engine. In his
grant proposal, Babbage gave the formula x2 + x + 41 as an example of a function that his
computer would tabulate. This particular function was of interest to mathematicians because
it produces an unusual number of prime numbers.Primes that have this form n = x2 + x + 41
for some integer x could be called Babbage primes. Write a program that finds all the Babbage
primes that are less than 10,000. Your first
five lines of output should look like this:
Output:
0 41 is prime
1 43 is prime
2 47 is prime
3 53 is prime
4 61 is prime
1 43 is prime
2 47 is prime
3 53 is prime
4 61 is prime
Solution:
public class TestBabbage {
public static void main(String[] args) {
Primes.setSize(1000);
for (int x = 0; x < 50; x++) {
System.out.print(x);
int n = x*x + x + 41;
if (Primes.isPrime(n)) {
System.out.println("\t"+n+" is prime");
} else {
System.out.println();
}
}
}
}
No comments :
Post a Comment