Need help for this project Please!

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 739 Views - Last Post: 21 March 2009 - 08:53 AM Rate Topic: -----

#1 jinggessler  Icon User is offline

  • New D.I.C Head

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

Need help for this project Please!

Post icon  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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#4 jinggessler  Icon User is offline

  • New D.I.C Head

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

Re: Need help for this project Please!

Posted 20 March 2009 - 08:50 AM

View Postmostyfriedman, 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
Was This Post Helpful? 0
  • +
  • -

#5 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 jinggessler  Icon User is offline

  • New D.I.C Head

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

Re: Need help for this project Please!

Posted 20 March 2009 - 09:08 AM

View Postscottyadam, 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?


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...
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • 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

View Postscottyadam, 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
Was This Post Helpful? 0
  • +
  • -

#8 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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) ) );

			}			

		}


Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Need help for this project Please!

Posted 20 March 2009 - 09:59 AM

View Postscottyadam, 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
Was This Post Helpful? 0
  • +
  • -

#10 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#12 DillonSalsman  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#13 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#14 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#15 jinggessler  Icon User is offline

  • New D.I.C Head

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

Re: Need help for this project Please!

Posted 20 March 2009 - 03:39 PM

:wub: 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!!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2