-
October 31st, 2011, 02:13 PM
#1
College lab help
Hi all. I am new and in need of some help. I'm sure it is something easy but I have been staring at it too long. Basically I need to write code that will display the prime factors of an inputted number. I am able to find the factors of the number pretty easily but I am getting bogged down testing if the factors are prime and then printing them. Here is what I have so far:
import java.util.Scanner;
public class lab63 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println ("Enter a number to factor: ");
int number = input.nextInt();
int x = 0;
System.out.print("Factors: ");
while (x<number){
int divisor = 1+x;
if (number%divisor==0){
System.out.print (" "+divisor);
}
x++;
}
System.out.println ();
}
This will find and display the factors but I need to add a loop it so it will find which of these factors are prime and display then. Any help would be really appreciated. Thanks.
-
October 31st, 2011, 04:14 PM
#2
Re: College lab help
Do you have an algorithm for testing if a number is a prime number?
-
November 1st, 2011, 08:06 PM
#3
Re: College lab help
Do a loop within the loop. Basically just do what you did to the numbers to find what is prime and then loop to do the same thing to those numbers.
-
November 1st, 2011, 08:08 PM
#4
Re: College lab help
Originally Posted by keang
Do you have an algorithm for testing if a number is a prime number?
Code:
class Prime_number {
public static void main(String[] args) {
int num = 11;
int i;
for (i=2; i < num ;i++ ){
int n = num%i;
if (n==0){
System.out.println("not Prime!");
break;
}
}
if(i == num){
System.out.println("Prime number!");
}
}
}
-
November 2nd, 2011, 04:57 AM
#5
Re: College lab help
There are more efficient ways of establishing if a number is prime.
You don't need to test if the number divides by 4,6,8... because it will also be divisble by 2.
You don't need to test all the way up to the number itself - the square root of the number is the largest divisor you need to check.
See http://www.mkyong.com/java/how-to-de...umber-in-java/
-
November 2nd, 2011, 05:04 AM
#6
Re: College lab help
Ok so now you have your algorithm for finding if the number is prime or not (although I suggest you amend it as AlexVV has suggested) place this code in a method in your lab63 class declared something like:
Code:
private static boolean isPrime(int num)
and in your main method call isPrime() whenever you want to test if a number is prime.
-
November 2nd, 2011, 09:17 PM
#7
Re: College lab help
You don't really need to determine if the numbers are prime.
I would suggest, however, starting from 2, which is the first prime number.
Also remember that a divisor might be used multiple times.
Example: The prime factors of 12 are 2, 2, and 3. IE: 2 * 2 * 3 = 12.
When the remainder is zero, you might want to (1) print the divisor, (2) divide number by the divisor and store the new smaller value back into number, and (3) try that same divisor again. You might want to increment the divisor only when the remainder is non-zero.
-
April 12th, 2018, 08:04 AM
#8
Re: College lab help
Below is the code,
PHP Code:
import java.util.*;
public class JavaPrimeNumber
{
public static void main(String[] args)
{
int digit;
boolean checkPrime = true;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number find to prime number in java : ");
int number = sc.nextInt();
for(int a = 2; a <= number / 2; a++)
{
digit = number % a;
if(digit == 0)
{
checkPrime = false;
break;
}
}
if(checkPrime)
System.out.println(number + " is a prime number.");
else
System.out.println(number + " is not a prime number.");
sc.close();
}
}
For more you can refer this resource for more on java programs.
Last edited by sidsomashak; April 12th, 2018 at 09:21 AM.
Reason: Have indented the code.
-
April 12th, 2018, 09:11 AM
#9
Re: College lab help
@sidsomashak This is a 7 year old thread.
You should wrap all posted code in code tags to preserve formatting and make the code easier to read.
Norm
-
April 12th, 2018, 10:17 AM
#10
Re: College lab help
The for loop can also be improved. Apart from 2, all prime numbers are odd. Also, the bound for checking for divisors should be sqrt, not half (calculate before the loop). So an improvement is to have a special check for 2 as a divisor first. If not, then the start of the loop should be 3 and the increment should be +2 up until sqrt of number.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|