16 Replies  794 Views  Last Post: 21 March 2009  08:53 AM
#1
Need help for this project Please!
Posted 20 March 2009  08:38 AM
Write a Java program that calculates the following product.
(1+1/2^2) (1+1/3^2)(1+1/5^2)(1+1/7^2).....(1+1/(P(n))^2)
the #s 2,3,4,5,....Pn are the 1st primes. Prompt the user to enter the integer n. Print the result as a double value. use dialog boxes for input/output perations.
can anyone give me any suggestion?
is there any formular can do this problem?
Thank you very much!
Replies To: Need help for this project Please!
#2
Re: Need help for this project Please!
Posted 20 March 2009  08:41 AM
#3
Re: Need help for this project Please!
Posted 20 March 2009  08:50 AM
http://www.computing...java/11287.html
static public IsPrime(int number) { if (number == 1  number == 2) return 1; for (int i=2; i<(int)(number/2); i++)//assumes number is unsigned...I don't even know that you *can* have unsigned numbers in Java...in either case, I'm sure there's a Math.abs() or something similar that you could use { if ( (number/i)==(int)(number/i) ) return 0; } return 1; }
#4
Re: Need help for this project Please!
Posted 20 March 2009  08:50 AM
mostyfriedman, on 20 Mar, 2009  07:41 AM, said:
I know how to generate the prime number ( kind of)
but I dont know how to put them together ... i mean I dont know how to do the rest part of it. my mind is not clear I guess, I will go back to think again.
anyway thank you
#5
Re: Need help for this project Please!
Posted 20 March 2009  09:02 AM
Quote
do you mean you don't know how to print to screen or do do calculations in java?
#6
Re: Need help for this project Please!
Posted 20 March 2009  09:08 AM
scottyadam, on 20 Mar, 2009  08:02 AM, said:
Quote
do you mean you don't know how to print to screen or do do calculations in java?
thanks for reply
I meant that I dont know how to do the calculations..I kept think there should be a fomular for that equation...
#7
Re: Need help for this project Please!
Posted 20 March 2009  09:22 AM
scottyadam, on 20 Mar, 2009  07:50 AM, said:
http://www.computing...java/11287.html
static public IsPrime(int number) { if (number == 1  number == 2) return 1; for (int i=2; i<(int)(number/2); i++)//assumes number is unsigned...I don't even know that you *can* have unsigned numbers in Java...in either case, I'm sure there's a Math.abs() or something similar that you could use { if ( (number/i)==(int)(number/i) ) return 0; } return 1; }
there's a more efficient way to do this btw
#8
Re: Need help for this project Please!
Posted 20 March 2009  09:56 AM
public static boolean isPrime(int num){ if (num == 1) return false; // Not prime for( int i = 2; i < num; i++ ) { if (num % i == 0) return false; // Not prime if (num % i != 0) return true; // Prime } return false; // won't reach here }
you might wanna do something like this for the calculations....I don't know if this would even work cause I hacked it out pretty fast....
for( int i = number; i > 2; i ){ if(isPrime(i)){ answer = ( (1+1)/( Math.pow( 2, i) ) ); } }
#9
Re: Need help for this project Please!
Posted 20 March 2009  09:59 AM
scottyadam, on 20 Mar, 2009  08:56 AM, said:
public static boolean isPrime(int num){ if (num == 1) return false; // Not prime for( int i = 2; i < num; i++ ) { if (num % i == 0) return false; // Not prime if (num % i != 0) return true; // Prime } return false; // won't reach here }
you might wanna do something like this for the calculations....I don't know if this would even work cause I hacked it out pretty fast....
for( int i = number; i > 2; i ){ if(isPrime(i)){ answer = ( (1+1)/( Math.pow( 2, i) ) ); } }
the isPrime() wont work for some cases actually
#10
Re: Need help for this project Please!
Posted 20 March 2009  10:05 AM
which ones....
at a quick glance how does the rest look?
OR BETTER YET...since I kinda came up with this...maybe i'll let jinggessler find out what's wrong...that way jinggessler can better understand what's going on :) (put this into a SSCCE and your in business!!)
SSCCE  Short SelfContained Compilable Example
This post has been edited by scottyadam: 20 March 2009  10:13 AM
#11
Re: Need help for this project Please!
Posted 20 March 2009  10:13 AM
#12
Re: Need help for this project Please!
Posted 20 March 2009  11:59 AM
So instead of i++ing , cut the process in half with i+=2.
Just my opinion
EDIT:
A little bit more detail,
if(num == 2) return true; else if(num%2==0) return false; //then check for all the odds.
This post has been edited by DillonSalsman: 20 March 2009  12:04 PM
#13
Re: Need help for this project Please!
Posted 20 March 2009  12:13 PM
#14
Re: Need help for this project Please!
Posted 20 March 2009  12:17 PM
public static boolean isPrime(int n) { if( n == 2 ) return true; if( n < 2  n % 2 == 0 ) return false; for(int i = 3; i*i <= n; i += 2) { if( n % i == 0 ) return false; } return true; }
#15
Re: Need help for this project Please!
Posted 20 March 2009  03:39 PM
I want to thank all your help, I will use all your advices to rework on my project again.
Thank you Thank you!!
