Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Computing the sum and prime number help.

807606Mar 19 2007 — edited Mar 21 2007
I have to compute the sum of 1/2 - 1/3 + 1/5...+ (-1)^n+1 * 1/p (where p is the nth prime)
I have done a similar program where i compute the sum 1- 1/2 +1/3...(-1)^n+1 * 1/n. that was fairly simple with a for loop, i've tried to incorporate the design of that program into the new one but with no luck. the old program is

import java.util.Scanner;

public class Project4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/**
* Get input from user.
*/
System.out.println("Enter an integer");
int n = input.nextInt();
double newNum;
double s = 0;
/**
* create a loop
*/
for(int i = 1; i <= n; i++){
/**
* make conditions
*/
if (i%2 == 0)
newNum = -1 * (1.0/i);
else
newNum = 1.0/i;
/**
* add the sum
*/
s = s + newNum;
}
/**
* print sum
*/
System.out.println(" The sum is " +s);

}

}


the new one i've been trying to make hasn't worked like i planned so i've been messing with variables and whatnot so now it looks kinda messed up and well...its probably useless. i know what a prime number is, where it is only divisible by 1 and itself and i have been reading other material on here about primes but none of it is quite like my situation, any help would be great (my first programming class)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 18 2007
Added on Mar 19 2007
23 comments
449 views