Need help for this project Please!

• (2 Pages)
• 1
• 2

16 Replies - 903 Views - Last Post: 21 March 2009 - 08:53 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=94177&amp;s=6489ad5cd0302ac9abdbbbaa365845ee&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 jinggessler

Reputation: 0
• Posts: 27
• Joined: 26-January 09

Need help for this project Please!

Posted 20 March 2009 - 08:38 AM

I have a project that need help from everyone:

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!

Is This A Good Question/Topic? 0

Replies To: Need help for this project Please!

#2 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

Re: Need help for this project Please!

Posted 20 March 2009 - 08:41 AM

you need a way to generate prime numbers, then the rest is easy

Reputation: 2
• Posts: 116
• Joined: 03-December 07

Re: Need help for this project Please!

Posted 20 March 2009 - 08:50 AM

Simple search on google took care of prime numbers:

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 jinggessler

Reputation: 0
• Posts: 27
• Joined: 26-January 09

Re: Need help for this project Please!

Posted 20 March 2009 - 08:50 AM

mostyfriedman, on 20 Mar, 2009 - 07:41 AM, said:

you need a way to generate prime numbers, then the rest is easy

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

Reputation: 2
• Posts: 116
• Joined: 03-December 07

Re: Need help for this project Please!

Posted 20 March 2009 - 09:02 AM

Quote

but I dont know how to put them together ... i mean I dont know how to do the rest part of it.

do you mean you don't know how to print to screen or do do calculations in java?

#6 jinggessler

Reputation: 0
• Posts: 27
• Joined: 26-January 09

Re: Need help for this project Please!

Posted 20 March 2009 - 09:08 AM

scottyadam, on 20 Mar, 2009 - 08:02 AM, said:

Quote

but I dont know how to put them together ... i mean I dont know how to do the rest part of it.

do you mean you don't know how to print to screen or do do calculations in java?

I meant that I dont know how to do the calculations..I kept think there should be a fomular for that equation...

#7 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

Re: Need help for this project Please!

Posted 20 March 2009 - 09:22 AM

i dont think there's a formula to generate prime number, otherwise we wouldn't need prime number algorithms...my advice is to check out the sieve of eratosthenes algorithm, this will give you the prime numbers in an array, then you can loop through that array and do the works

scottyadam, on 20 Mar, 2009 - 07:50 AM, said:

Simple search on google took care of prime numbers:

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

Reputation: 2
• Posts: 116
• Joined: 03-December 07

Re: Need help for this project Please!

Posted 20 March 2009 - 09:56 AM

ok so I sort of redid the prime function...if that would help

```	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 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

Re: Need help for this project Please!

Posted 20 March 2009 - 09:59 AM

scottyadam, on 20 Mar, 2009 - 08:56 AM, said:

ok so I sort of redid the prime function...if that would help

```	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

Reputation: 2
• Posts: 116
• Joined: 03-December 07

Re: Need help for this project Please!

Posted 20 March 2009 - 10:05 AM

oh...really?

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 Self-Contained Compilable Example

This post has been edited by scottyadam: 20 March 2009 - 10:13 AM

#11 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

Re: Need help for this project Please!

Posted 20 March 2009 - 10:13 AM

just remove the 2nd if statement in the loop, and make the last return statement to return true

#12 DillonSalsman

Reputation: 13
• Posts: 144
• Joined: 30-October 07

Re: Need help for this project Please!

Posted 20 March 2009 - 11:59 AM

Just so you know, if a number is not divisible by 2, its not divisible by any other even number.
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

Reputation: 2
• Posts: 116
• Joined: 03-December 07

Re: Need help for this project Please!

Posted 20 March 2009 - 12:13 PM

very true...another thing for jinggessler to think about

#14 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

Re: Need help for this project Please!

Posted 20 March 2009 - 12:17 PM

you also need to check up to the root of the number because the number of divisors of a number less than its root are equal to the number of divisors more than its root
```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 jinggessler

Reputation: 0
• Posts: 27
• Joined: 26-January 09

Re: Need help for this project Please!

Posted 20 March 2009 - 03:39 PM

wow, few hours of time I have got these many help!!!!

I want to thank all your help, I will use all your advices to rework on my project again.

Thank you Thank you!!