Testing all prime #'s in an if case until reaching a certain limit.

Hi there,

I've been working on this radical simplifier program that simplifies square roots. There is one point in the program where the code checks to see if the user entered a pure square root that is odd (9,25,49,81 etc), yet in order to properly test it, I need to somehow implement a prime # generator into the if case, and let it test each prime # unitil it gets to some big # like 1,000,000. I have comments at that certain location and have supplied both a prime # checking method and a prime # generating method. I got both methods off some free sample math code websites, and so some of their coding is a little advanced for me. Am I going about this correctly or should I code this another way? Also, any suggestions to improve it would be great. Thanks!

P.S. Hopefully my 4 space indents don't make you crazy. Sorry about that one!

import javax.swing.JOptionPane;

import java.lang.Math;

import java.awt.*;

publicclass squareRoot

{

staticint prime = 5;

staticlong root;

publicstaticvoid main(String[] args)

{

boolean calculating =true;

while (calculating)

{

String entry = JOptionPane.showInputDialog(null,

"Enter a square root to simplify.",

"Square Root Simplifier",

JOptionPane.INFORMATION_MESSAGE);

root = Long.parseLong(entry);

long testPrime = root;

long divisor = 1;

divisor = isPrime(testPrime);

if (divisor == testPrime)

JOptionPane.showMessageDialog(null,

"Your number can't be simplified any further: " + root +" is prime!",

"Square Root Simplifier",

JOptionPane.INFORMATION_MESSAGE);

else

{

if ((root % 2) == 0)//check if it's even

{

if ((Math.sqrt(root) % 2) == 0)//check if it's a pure square root

{

int square = (int)Math.sqrt(root);

JOptionPane.showMessageDialog(null,

"The square root of " + root +" is " + square,

"Square Root Simplifier",

JOptionPane.INFORMATION_MESSAGE);

}

else

{

testPrime = root;

long multiplier = 0;

while (testPrime > 0)

{

testPrime -= 2;

multiplier = testPrime;

multiplier *= multiplier;

if (multiplier < root)

{

if (multiplier == 0)

{

JOptionPane.showMessageDialog(null,

"The square root of " + root +" is already simplified!",

"Square Root Simplifier",

JOptionPane.INFORMATION_MESSAGE);

break;

}

if ((root % multiplier) == 0)

{

testPrime = root / multiplier;

multiplier = (long) Math.sqrt(multiplier);

JOptionPane.showMessageDialog(null,

"The square root of " + root +" simplified is:\n" + multiplier +" times the square root of " + testPrime,

"Square Root Simplifier",

JOptionPane.INFORMATION_MESSAGE);

break;

}

}

}

}

//calculating = false;

}

else//check if it's odd

{

while (prime <= 99999)

{

if ((Math.sqrt(root) % 3 == 0) ||//check if it's a pure square root

(Math.sqrt(root) % 5 == 0) ||

(Math.sqrt(root) % 7 == 0) ||

(Math.sqrt(root) % 11 == 0) ||

(Math.sqrt(root) % 13 == 0))

{//I notice that it always mods it by

int square = (int)Math.sqrt(root);//a prime #. How could I use

JOptionPane.showMessageDialog(null,//genPrime() to do this part?

"The square root of " + root +" is " + square,//Or is there another better way of testing

"Square Root Simplifier",//the primes?

JOptionPane.INFORMATION_MESSAGE);

break;

}

//prime = genPrime();

}

if (prime >= 99999)

{

testPrime = root;

long multiplier = 0;

while (testPrime > 0)

{

testPrime -= 3;

multiplier = testPrime;

multiplier *= multiplier;

if (multiplier < root)

{

if (multiplier == 0)

{

JOptionPane.showMessageDialog(null,

"The square root of " + root +" is already simplified!",

"Square Root Simplifier",

JOptionPane.INFORMATION_MESSAGE);

break;

}

if ((root % multiplier) == 0)

{

testPrime = root / multiplier;

multiplier = (long) Math.sqrt(multiplier);

if (multiplier == 1)

{

JOptionPane.showMessageDialog(null,

"The square root of " + root +" is already simplified!",

"Square Root Simplifier",

JOptionPane.INFORMATION_MESSAGE);

break;

}

else

{

JOptionPane.showMessageDialog(null,

"The square root of " + root +" simplified is:\n" + multiplier +" times the square root of " + testPrime,

"Square Root Simplifier",

JOptionPane.INFORMATION_MESSAGE);

break;

}

}

}

}

}

//calculating = false;

}

}

}

}

//Prime # determiner by Kevin Gong

publicstaticlong isPrime(long testPrime)

{

long test;

test = 2;// should at least use square root

while ( test < testPrime )

{

if ( testPrime % test == 0 )

return test;

if ( test == 2 )

test++;

else

test += 2;

}

return testPrime;

}

//Prime # generator

publicstaticint genPrime()

{

int [] pArray =newint[1000];//primes to check against

int i, num = 0;//num = number of primes saved

boolean check =true;

pArray[0] = 3;//store initial prime

int index = 0, test1 = 3;//first number to test is 5

while(test1 < (pArray[num] * pArray[num]) && (Math.sqrt(root) % prime == 0))

{

index = 0;

check =true;//reset flags

while(check ==true && index <= num && test1 >= (pArray[index] * pArray[index]))

{if(test1 % pArray[index] == 0)

check =false;

else index++;

}

if(check ==true)//found prime

{

if(num < (pArray.length - 1))

pArray[++num] = test1;//save prime

break;

}

test1 += 2;

//if((test1 - 1) % 100000 == 0)

//System.out.println("");//clear every 100,000

}

return test1;

}

}

[11862 byte] By [Jason102a] at [2007-10-2 10:59:10]
# 1
I've moved this thread to http://forum.java.sun.com/thread.jspa?threadID=703587&tstart=0Please post your stuff there from now on.
Jason102a at 2007-7-13 3:27:25 > top of Java-index,Other Topics,Algorithms...