Monday, September 06, 2004

The Code that Saved the Day

Long weekend. Pleanty of time to forget school. Did I do my homework until today? No. Was it a good idea? No. I am sure that I will still be working on discrete structures for a few more hours, however, I feel like such a smart little budding CS student that I thought I would post my happy solution. I am sure that any programmer with half of a brain cell would have figured this out faster than me, maybe there is even a better way to prove it with out compters, but I didn't find it.

The problem:
Consider the polynomial n^2 + n + 41 Disprove: If n is a positive integer, then n^2 + n + 41 is prime.

Here is my answer (the programming):
public class Proof {

static int n = 10;
static boolean prime = true;
static double formula;
static double mod;

public static void test (){
while (prime == true){
formula = (n*n) + n + 41;
double a = formula / formula;
double b = formula / 1;

for (int i=2; i<>{
mod = formula % i;
if (mod == 0.00) {
double div = formula / i;
System.out.println( i + " divides non-prime " + div + " times");
prime = false;
}
}
if (a == 1 &&amp;amp;amp;amp; b == formula && prime != false){
System.out.println("Value of n: " + n + " Value of prime: " + formula + " A: "+ a + " B: " + b); } n = n + 1;
}
n= n - 1;
System.out.println("Value of n: " + n + " Value of non-prime: " + formula);
}

public static void main (String[] args){
test();
}
}

Here is the output:
C:\CS2100>javac Proof.java

C:\CS2100>java Proof
Value of n: 10 Value of prime: 151.0 A: 1.0 B: 151.0
Value of n: 11 Value of prime: 173.0 A: 1.0 B: 173.0
Value of n: 12 Value of prime: 197.0 A: 1.0 B: 197.0
Value of n: 13 Value of prime: 223.0 A: 1.0 B: 223.0
Value of n: 14 Value of prime: 251.0 A: 1.0 B: 251.0
Value of n: 15 Value of prime: 281.0 A: 1.0 B: 281.0
Value of n: 16 Value of prime: 313.0 A: 1.0 B: 313.0
Value of n: 17 Value of prime: 347.0 A: 1.0 B: 347.0
Value of n: 18 Value of prime: 383.0 A: 1.0 B: 383.0
Value of n: 19 Value of prime: 421.0 A: 1.0 B: 421.0
Value of n: 20 Value of prime: 461.0 A: 1.0 B: 461.0
Value of n: 21 Value of prime: 503.0 A: 1.0 B: 503.0
Value of n: 22 Value of prime: 547.0 A: 1.0 B: 547.0
Value of n: 23 Value of prime: 593.0 A: 1.0 B: 593.0
Value of n: 24 Value of prime: 641.0 A: 1.0 B: 641.0
Value of n: 25 Value of prime: 691.0 A: 1.0 B: 691.0
Value of n: 26 Value of prime: 743.0 A: 1.0 B: 743.0
Value of n: 27 Value of prime: 797.0 A: 1.0 B: 797.0
Value of n: 28 Value of prime: 853.0 A: 1.0 B: 853.0
Value of n: 29 Value of prime: 911.0 A: 1.0 B: 911.0
Value of n: 30 Value of prime: 971.0 A: 1.0 B: 971.0
Value of n: 31 Value of prime: 1033.0 A: 1.0 B: 1033.0
Value of n: 32 Value of prime: 1097.0 A: 1.0 B: 1097.0
Value of n: 33 Value of prime: 1163.0 A: 1.0 B: 1163.0
Value of n: 34 Value of prime: 1231.0 A: 1.0 B: 1231.0
Value of n: 35 Value of prime: 1301.0 A: 1.0 B: 1301.0
Value of n: 36 Value of prime: 1373.0 A: 1.0 B: 1373.0
Value of n: 37 Value of prime: 1447.0 A: 1.0 B: 1447.0
Value of n: 38 Value of prime: 1523.0 A: 1.0 B: 1523.0
Value of n: 39 Value of prime: 1601.0 A: 1.0 B: 1601.0
Value of n: 40 Value of prime: 1681.0 A: 1.0 B: 1681.0
Value of n: 41 Value of prime: 1763.0 A: 1.0 B: 1763.0
Value of n: 42 Value of prime: 1847.0 A: 1.0 B: 1847.0
Value of n: 43 Value of prime: 1933.0 A: 1.0 B: 1933.0
43 divides non-prime 47.0 times
Value of n: 44 Value of non-prime: 2021.0

C:\CS2100>


Well... the formatting is all messed up. I really should spend some time here so taht I can make formatted code part of my style sheet. Anyway, cheers to the number 44 for making my homework problem go away!


No comments: