Product of Prime Numbers

Hello,

Im trying to write another method called primeProduct that takes as input a positive integer n > 1and returns the product of the primes from 2 to n.

So far i have only got the program to see if the number is prime or not.

Is it easier to write primeProduct in an array? And is the formulae to get primeProduct

product = product * n

Here is my code thus far:

import java.io.*;

public class primeProduct {

public static void main (String[] args)

throws IOException{

primeProduct app = new primeProduct();

app.doit();

}

private void doit() throws IOException{

BufferedReader in = new BufferedReader(

new InputStreamReader (System.in));

int n = Integer.parseInt(in.readLine());

if (n > 0){

isPrime(n);

}

}

private void isPrime(int n) {

int c = 2;

while (c * c < n && n % c != 0){

c = c + 1;

}

if (n == 2 || n > 2 && n % c !=0){

System.out.print("True.");

}else{

System.out.print("False.");

}

}

}

[1127 byte] By [Vmastaa] at [2007-10-2 15:42:58]
# 1

here is what i got to multiply all the numbers < given number

public int primeProduct(int n) {

int product = 1;

for (int k = 1; k <= n; k++){

product *= k;

}

System.out.println(product);

return product;

}

so if i type in 5, it goes 1*2*3*4*5 and gives 120.

How do i get it to only multiply the prime numbers eg. 2*3*5?

Vmastaa at 2007-7-13 15:31:52 > top of Java-index,Other Topics,Algorithms...
# 2

Good progress.. just check if each number is a prime before multiplying.

public int primeProduct(int n) {

int product = 1;

for (int k = 1; k <= n; k++){

if (isPrime(k) == true) {

product *= k;

}

}

System.out.println(product);

return product;

}

private boolean isPrime(int n) {

int c = 2;

while (c * c < n && n % c != 0){

c = c + 1;

}

if (n == 2 || n > 2 && n % c !=0){

// System.out.print("True.");

return true;

}else{

//System.out.print("False.");

return false;

}

ordinary_guya at 2007-7-13 15:31:52 > top of Java-index,Other Topics,Algorithms...
# 3

I'm trying to get the output so the number entered by the user shows up either as true being prime, and false being not prime aswell as giving the primeProduct result..

Currently if i put in 5 as the number the output would be:

5<-number entered by user

30<-prime product

Though i want it to say if the number is prime:

5 Is a prime number.

30

I tried to put the System.out.println("Is a prime number");

and System.out.println("Is not a prime number");

but anywhere i put it throws an error. Any ideas?

Heres what i got so far:

import java.io.*;

public class primeProduct1 {

int product = 1;

int cprime;

public static void main (String[] args)

throws IOException{

primeProduct1 app = new primeProduct1();

app.doit();

}

private void doit() throws IOException{

BufferedReader in = new BufferedReader(

new InputStreamReader (System.in));

int n = Integer.parseInt(in.readLine());

if (n > 0){

primeProduct(n);

}

}

private boolean isPrime(int n) {

int c = 2;

while (c * c < n && n % c != 0){

c = c + 1;

}

if (n == 2 || n > 2 && n % c !=0){

// System.out.print("Is prime.");

return true;

}else{

//System.out.print("Is not prime.");

return false;

}

}

public int primeProduct(int n) {

int product = 1;

for (int k = 1; k <= n; k++){

if (isPrime(k) == true) {

product *= k;

}

}

System.out.println(product);

return product;

}

}

Vmastaa at 2007-7-13 15:31:52 > top of Java-index,Other Topics,Algorithms...