Sum of prime numbers taken from the command-line

In summary: However, because there are only three prime numbers in that sequence, the program would stop there.In summary, the program prints out the primes in a given sequence and stops when it reaches a three-prime number.
  • #1
Hiche
84
0

Homework Statement



Write a program that takes a command line argument N and a sequence of N positive integers and prints the numbers that are prime only, followed by their sum.

Homework Equations



for loop

The Attempt at a Solution



This has been a vexing program to think of. We were asked to write a program that takes an integer N and outputs true if N is prime and false if N is not prime. The code is as follows:

Code:
public class Primes
{
	public static void main(String[] args)
	{
		int numb = Integer.parseInt(args[0]);
		int i;
		boolean ifPrime;
		
		for (i = 2; i < numb; i++)
		{
			int n = numb % i;
			if (n == 0)
			{
				ifPrime = false;
				System.out.println(numb + " is not a prime number, hence " + ifPrime);
				break;
			}
		}
			if (i == numb)
			{
				ifPrime = true;
				System.out.println(numb + " is a prime number, hence " + ifPrime);
			}
	}
}

Although we haven't actually covered the break command, but I didn't know how to code it without it.

Now, the question above is a little confusing to me, particularly the wording. Does it want to take a command-line value N then N numbers afterward or is it something else?

The way I thought of it is to first write a method of type boolean that studies the integer(s) and then returns true or false if prime or not. We enter a loop in the main method that loops over args where i is an initialization variable (int), then use if to compare the returned boolean value if. But here's where I hit the wall. What's next? How do you continue from here?

This is my last question, I hope.
 
Physics news on Phys.org
  • #2
As far as the break line. You proved it false, so the break would kill the for loop. This would be a faster and optimal code to get it out of that for loop.
Take numb = 1 billion, 2 would prove it not prime. Your break command got it out of that for loop within 1 iteration. However without it, you would loop for 1 billion iterations.
You can just keep proving it false over and over. Doesn't hurt. Not on the scale your doing it. Highly doubt your teacher cares. :)

Now key thing here, you just proved it false. Doesn't matter if "i" equals the original number. You already know its false. You could of finished the for loop and took "i" to the full original value.

You exit the for loop with isPrime = false;

Use that boolean.
 
  • #3
As far as the command line is concerned, the idea is that you would start your program something like this:

Code:
C:\Primes 5 12 13 28 5 61
The first number, 5, indicates how many numbers there are in the sequence following it. The five numbers after 5 are the sequence that your program should operate on.

Your program should display the primes in that sequence; namely 13, 5, and 61
 

1. What is the "Sum of prime numbers taken from the command-line"?

The "Sum of prime numbers taken from the command-line" refers to a mathematical operation where the sum of all the prime numbers entered in the command-line is calculated.

2. How do you calculate the sum of prime numbers from the command-line?

To calculate the sum of prime numbers from the command-line, you first need to enter all the prime numbers as command-line arguments. Then, using a programming language or a calculator, you can add all the numbers together to find the sum.

3. What are prime numbers?

Prime numbers are positive integers that are only divisible by 1 and themselves. In other words, they have no other factors other than 1 and themselves.

4. Why is the sum of prime numbers from the command-line important?

The sum of prime numbers from the command-line is important because it can help in solving many mathematical problems and can also be used as a benchmark for testing the efficiency of algorithms and programming languages.

5. Can the sum of prime numbers from the command-line be negative?

No, the sum of prime numbers from the command-line cannot be negative because prime numbers are always positive integers. If any negative numbers are entered as command-line arguments, they will not be included in the sum of prime numbers calculation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
  • Programming and Computer Science
Replies
22
Views
728
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top