Computer Science Java Programming Help

In summary: Also, why are you reposting the same code?In summary, the conversation discussed an algorithm for determining whether an integer is a prime number, using the conditions "if x is divisible by y" and the Java code "((x % y) == 0)". The algorithm involves starting with the assumption that the number is prime and then testing for divisibility by every number from 2 to the square root of the input number. The conversation also mentioned the importance of not testing for divisibility by 1 and taking into account multiples of previously tested divisors. A simple Java code was provided as an example of the algorithm.
  • #1
JasonJo
429
2
Computer Science Java Programming Help!

Determine whether or not an integer given by the user is a prime number. You may assume that the number is greater than or equal to 2. You may conditions that are in the form “if x is divisible by y” when writing your algorithm.

HINT: One way of solving this problem is to start with the assumption that the number is going to be prime. If you find at any point in your algorithm that this is not the case you should print a message saying it isn’t and then immediately exit the program (Halt). Otherwise, print a message that the number is prime at the end of your program before exiting.

NOTE: If you choose to use this algorithm for writing your program in Question 2, the Java code “((x % y) == 0)” will be true if and only if x is divisible by y (it literally means that the remainder when x is divided by y is equal to zero). It might also be helpful to know that the line of code “System.exit(0);” can be used to terminate the program at any point you wish.

my problem is:
lets say the number input is 9000203. So my algorithm would be, see if input is divisible by: 1,2,3,4,5,6?

any help on constructing this algorithm? thanks
 
Physics news on Phys.org
  • #2
JasonJo said:
my problem is:
lets say the number input is 9000203. So my algorithm would be, see if input is divisible by: 1,2,3,4,5,6?

any help on constructing this algorithm? thanks

Well if you're going to do that (which is probably the way I'd do it as well), then where's the problem with your algorithm? All you really need to do is ask yourself what you can say in both cases (ie. whether or not the input is divisible exactly by 1, 2, 3,...).
 
  • #3
DON'T TEST "1". Every number is divisible by 1, this won't give you any information, and depending on your program, it may give you wrong information. Also, how high do you plan on going up? I'm sure you plan on going past 6, but how high? If N is the input number, you don't have to go all the way up to N. Also, if you've tested if N is divisible by n, and it turns out that it's not, then do you need to test if it's divisible by 2n, 3n, 4n, ...? And of course, if your number is divisible by n, you'll halt. For 19, it suffices to check whether it's divisible by 2 or 3. If not, then that's enough to say it's prime.
 
  • #4
go look up prime factorization...teh simplest algorithm.
 
  • #5
would this algorithm be valid, I am using psuedo code

Get a value for n
Set divisor to n-1
If n = 2
Then
Print the message "The number"
Print the value of n
Print "is a prime number."
Else
while (divisor >= 2) Do
If (n % divisor)
Then
Print the message "The number"
Print the value of n
Print "is not a prime number."
Else
Set divisor = divisor - 1
End if
End While
End if

Print the message "The number"
Print the value of n
Print "is a prime number."

this is a pretty inefficient algorithm, because if n isn't divisible by 2, it isn't divisible by any multiples of 2.
 
Last edited:
  • #6
If you want efficient algoritms you can get some heavy stuff:
http://mathworld.wolfram.com/PrimalityTest.html

That said, you could make things a good bit tighter and cleaner:

Code:
for(divisor=int(sqrt(divisor));divisor>1;divisor=divisor-1) {
   break if(n%divisor == 0);
}
if(divisor > 1) {
   print "The number n is not a prime";
} else {
//This would need an extra clause if you had to deal with numbers <=2.
   print "The number n is a prime";
}

Looking at what you've got, it looks like it would print "N is a prime number" for any number, and woudl spam "N is not a prime number" for numbers that are not prime.
 
  • #7
i am taking an intro to compsci class, and i basically haven't even been introduced to the break lingo or anything like that.

he doesn't mind if its inefficient.

anything you can suggest to change my algo that doesn't involve higher level stuff?
 
  • #8
The classic algorithm is very simple: test all numbers between 2 and sqrt(N), where N is the input number. If it passes every test (i.e. the remainder is never zero for any divisor), then it's prime.

- Warren
 
  • #9
UNIX Java code

public class prime
{
public static void main (String[] args)
{
int n, divisor;
n = Console.readInt("Please enter a value for n: ");
divisor = n-1;
while (divisor >= 2)
{
if (n % divisor==0 )
{
System.out.println("The number " + n + " is not a prime number.$
System.exit(0);
}
divisor = divisor - 1;
}
System.out.println("The number " + n + " is a prime number.");
}
}

i just compiled it and ran it


thanks for ur help guys! (IT WORKS!)
 
  • #10
JasonJo said:
UNIX Java code
Code:
public class prime
{
        public static void main (String[] args)
        {
        int n, divisor;
        n = Console.readInt("Please enter a value for n: ");
        divisor = n-1;
        while (divisor >= 2)
        {
                if (n % divisor==0      )
                {
                System.out.println("The number " + n + " is not a prime number.$
                System.exit(0);
                }
        divisor = divisor - 1;
        }
        System.out.println("The number " + n + " is a prime number.");
        }
}
i just compiled it and ran it


thanks for ur help guys! (IT WORKS!)

Please use the code tags, it makes things so much easier to read.
 

1. What is Java programming and how is it related to computer science?

Java programming is a popular programming language used to develop various applications and software. It is related to computer science because it is based on the principles of object-oriented programming, which is a fundamental concept in computer science.

2. Why is Java considered an important language in computer science?

Java is considered important in computer science because of its widespread use in various fields such as web development, mobile app development, and scientific computing. It is also known for its platform independence, meaning it can run on different operating systems without any modifications.

3. What are the key features of Java programming?

Some key features of Java programming include its object-oriented nature, platform independence, automatic memory management, and a large standard library. It also supports multithreading, making it suitable for developing concurrent applications.

4. What is the difference between Java and other programming languages?

Java differs from other programming languages in several ways. Unlike languages like C and C++, Java does not require manual memory management, reducing the risk of memory leaks. It also has a simpler syntax and is more beginner-friendly compared to other languages. Additionally, Java is platform-independent, while other languages may be specific to a particular operating system.

5. How can I improve my Java programming skills?

To improve your Java programming skills, it is important to practice regularly and work on different projects. You can also enroll in online courses or attend workshops to learn new concepts and techniques. It is also helpful to read books and articles on Java programming and participate in online communities to exchange knowledge and ideas with other programmers.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • STEM Academic Advising
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
758
  • Engineering and Comp Sci Homework Help
Replies
18
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top