Why is this program for finding Prime Factors of a number using Java not working?
up vote
0
down vote
favorite
I would like to know why this java program is not working to find the prime factors of a number. I have seen many solutions on this site and elsewhere but I want to know why this approach is not sufficient as it returns only 1 as an output? The first "if statement" handles numbers from 1 and lower to return -1 (invalid value), thanks.
public class PrimeFactors{
public static void main(String args) {
System.out.println(getPrimeFactors(4));
}
public static int getPrimeFactors(int number) {
if (number <= 1) {
return -1;
}
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
return i;
}
}
return number;
}
}
java
add a comment |
up vote
0
down vote
favorite
I would like to know why this java program is not working to find the prime factors of a number. I have seen many solutions on this site and elsewhere but I want to know why this approach is not sufficient as it returns only 1 as an output? The first "if statement" handles numbers from 1 and lower to return -1 (invalid value), thanks.
public class PrimeFactors{
public static void main(String args) {
System.out.println(getPrimeFactors(4));
}
public static int getPrimeFactors(int number) {
if (number <= 1) {
return -1;
}
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
return i;
}
}
return number;
}
}
java
3
Possible duplicate of What is a debugger and how can it help me diagnose problems?
– Hovercraft Full Of Eels
Nov 10 at 3:20
1
Please see the How to Ask page. Get out a pencil and paper and "play computer" for a moment: what's actually happening?
– Dave Newton
Nov 10 at 3:21
1
It seems that you have a fundamental misunderstanding of howreturn
works. I advise that you refer to a Java tutorial and look up either print statements or arrays, as they may provide a good alternative to the inner return statement depending on what you want your output to be.
– Andrew Fan
Nov 10 at 3:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to know why this java program is not working to find the prime factors of a number. I have seen many solutions on this site and elsewhere but I want to know why this approach is not sufficient as it returns only 1 as an output? The first "if statement" handles numbers from 1 and lower to return -1 (invalid value), thanks.
public class PrimeFactors{
public static void main(String args) {
System.out.println(getPrimeFactors(4));
}
public static int getPrimeFactors(int number) {
if (number <= 1) {
return -1;
}
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
return i;
}
}
return number;
}
}
java
I would like to know why this java program is not working to find the prime factors of a number. I have seen many solutions on this site and elsewhere but I want to know why this approach is not sufficient as it returns only 1 as an output? The first "if statement" handles numbers from 1 and lower to return -1 (invalid value), thanks.
public class PrimeFactors{
public static void main(String args) {
System.out.println(getPrimeFactors(4));
}
public static int getPrimeFactors(int number) {
if (number <= 1) {
return -1;
}
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
return i;
}
}
return number;
}
}
java
java
edited Nov 10 at 6:47
Abhinav
300111
300111
asked Nov 10 at 3:16
Tunde
43
43
3
Possible duplicate of What is a debugger and how can it help me diagnose problems?
– Hovercraft Full Of Eels
Nov 10 at 3:20
1
Please see the How to Ask page. Get out a pencil and paper and "play computer" for a moment: what's actually happening?
– Dave Newton
Nov 10 at 3:21
1
It seems that you have a fundamental misunderstanding of howreturn
works. I advise that you refer to a Java tutorial and look up either print statements or arrays, as they may provide a good alternative to the inner return statement depending on what you want your output to be.
– Andrew Fan
Nov 10 at 3:23
add a comment |
3
Possible duplicate of What is a debugger and how can it help me diagnose problems?
– Hovercraft Full Of Eels
Nov 10 at 3:20
1
Please see the How to Ask page. Get out a pencil and paper and "play computer" for a moment: what's actually happening?
– Dave Newton
Nov 10 at 3:21
1
It seems that you have a fundamental misunderstanding of howreturn
works. I advise that you refer to a Java tutorial and look up either print statements or arrays, as they may provide a good alternative to the inner return statement depending on what you want your output to be.
– Andrew Fan
Nov 10 at 3:23
3
3
Possible duplicate of What is a debugger and how can it help me diagnose problems?
– Hovercraft Full Of Eels
Nov 10 at 3:20
Possible duplicate of What is a debugger and how can it help me diagnose problems?
– Hovercraft Full Of Eels
Nov 10 at 3:20
1
1
Please see the How to Ask page. Get out a pencil and paper and "play computer" for a moment: what's actually happening?
– Dave Newton
Nov 10 at 3:21
Please see the How to Ask page. Get out a pencil and paper and "play computer" for a moment: what's actually happening?
– Dave Newton
Nov 10 at 3:21
1
1
It seems that you have a fundamental misunderstanding of how
return
works. I advise that you refer to a Java tutorial and look up either print statements or arrays, as they may provide a good alternative to the inner return statement depending on what you want your output to be.– Andrew Fan
Nov 10 at 3:23
It seems that you have a fundamental misunderstanding of how
return
works. I advise that you refer to a Java tutorial and look up either print statements or arrays, as they may provide a good alternative to the inner return statement depending on what you want your output to be.– Andrew Fan
Nov 10 at 3:23
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
The code you have here is returning the value of i, which you have set to 1. You don't have enough understanding of how you would find a prime number based on your code. No matter what you put into your method, getPrimeFactors will either return -1 or 1, because of how your code is set up. The remainder when a number is divided by 1 is always 0, therefore it is always true, which will just return 1. And if the number is less than or equal to 1, it will return -1. Return number is essentially dead code unless you fix the syntax. Hope this helps you out!
1
Yes it does, thanks a bunch.
– Tunde
Nov 10 at 3:47
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
The code you have here is returning the value of i, which you have set to 1. You don't have enough understanding of how you would find a prime number based on your code. No matter what you put into your method, getPrimeFactors will either return -1 or 1, because of how your code is set up. The remainder when a number is divided by 1 is always 0, therefore it is always true, which will just return 1. And if the number is less than or equal to 1, it will return -1. Return number is essentially dead code unless you fix the syntax. Hope this helps you out!
1
Yes it does, thanks a bunch.
– Tunde
Nov 10 at 3:47
add a comment |
up vote
3
down vote
The code you have here is returning the value of i, which you have set to 1. You don't have enough understanding of how you would find a prime number based on your code. No matter what you put into your method, getPrimeFactors will either return -1 or 1, because of how your code is set up. The remainder when a number is divided by 1 is always 0, therefore it is always true, which will just return 1. And if the number is less than or equal to 1, it will return -1. Return number is essentially dead code unless you fix the syntax. Hope this helps you out!
1
Yes it does, thanks a bunch.
– Tunde
Nov 10 at 3:47
add a comment |
up vote
3
down vote
up vote
3
down vote
The code you have here is returning the value of i, which you have set to 1. You don't have enough understanding of how you would find a prime number based on your code. No matter what you put into your method, getPrimeFactors will either return -1 or 1, because of how your code is set up. The remainder when a number is divided by 1 is always 0, therefore it is always true, which will just return 1. And if the number is less than or equal to 1, it will return -1. Return number is essentially dead code unless you fix the syntax. Hope this helps you out!
The code you have here is returning the value of i, which you have set to 1. You don't have enough understanding of how you would find a prime number based on your code. No matter what you put into your method, getPrimeFactors will either return -1 or 1, because of how your code is set up. The remainder when a number is divided by 1 is always 0, therefore it is always true, which will just return 1. And if the number is less than or equal to 1, it will return -1. Return number is essentially dead code unless you fix the syntax. Hope this helps you out!
answered Nov 10 at 3:41
Excel
478
478
1
Yes it does, thanks a bunch.
– Tunde
Nov 10 at 3:47
add a comment |
1
Yes it does, thanks a bunch.
– Tunde
Nov 10 at 3:47
1
1
Yes it does, thanks a bunch.
– Tunde
Nov 10 at 3:47
Yes it does, thanks a bunch.
– Tunde
Nov 10 at 3:47
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235722%2fwhy-is-this-program-for-finding-prime-factors-of-a-number-using-java-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Possible duplicate of What is a debugger and how can it help me diagnose problems?
– Hovercraft Full Of Eels
Nov 10 at 3:20
1
Please see the How to Ask page. Get out a pencil and paper and "play computer" for a moment: what's actually happening?
– Dave Newton
Nov 10 at 3:21
1
It seems that you have a fundamental misunderstanding of how
return
works. I advise that you refer to a Java tutorial and look up either print statements or arrays, as they may provide a good alternative to the inner return statement depending on what you want your output to be.– Andrew Fan
Nov 10 at 3:23