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;
}
}









share|improve this question




















  • 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

















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;
}
}









share|improve this question




















  • 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















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;
}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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










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














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!






share|improve this answer

















  • 1




    Yes it does, thanks a bunch.
    – Tunde
    Nov 10 at 3:47











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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!






share|improve this answer

















  • 1




    Yes it does, thanks a bunch.
    – Tunde
    Nov 10 at 3:47















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!






share|improve this answer

















  • 1




    Yes it does, thanks a bunch.
    – Tunde
    Nov 10 at 3:47













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!






share|improve this answer












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!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 3:41









Excel

478




478








  • 1




    Yes it does, thanks a bunch.
    – Tunde
    Nov 10 at 3:47














  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)