The fastest algorithm to solve equation with 2 unknown parametrs in c?











up vote
0
down vote

favorite












I am counting the value of possible combinations of x and y. It works but when I put big numbers it takes way too long. Do you have any ideas for better algorithm?



ax + by = c



The input of program is a, b and c, which should be non-negative numbers.
My code looks like this:



int combs=0;
for(int x=0; x < c; x++) {
for(int y=0; y < c; y++) {
if( (a*x) + (b*y) == c) {
combs++;
}
}
}









share|improve this question
























  • Are there limits on a, b, and c?
    – pstrjds
    Nov 11 at 15:11










  • They have to be bigger or equal to 0, thats all.
    – Petr Konecny
    Nov 11 at 15:12










  • What is the allowable range of x,y? Can x or y be negative?
    – chux
    Nov 11 at 16:13








  • 2




    en.wikipedia.org/wiki/Diophantine_equation
    – Matt Timmermans
    Nov 11 at 16:34






  • 1




    @Petr Konecny "it takes way too long" --> This can be solved without any iterations on x,y, yet looks like you have an acceptable solution already - too bad.
    – chux
    Nov 11 at 19:11















up vote
0
down vote

favorite












I am counting the value of possible combinations of x and y. It works but when I put big numbers it takes way too long. Do you have any ideas for better algorithm?



ax + by = c



The input of program is a, b and c, which should be non-negative numbers.
My code looks like this:



int combs=0;
for(int x=0; x < c; x++) {
for(int y=0; y < c; y++) {
if( (a*x) + (b*y) == c) {
combs++;
}
}
}









share|improve this question
























  • Are there limits on a, b, and c?
    – pstrjds
    Nov 11 at 15:11










  • They have to be bigger or equal to 0, thats all.
    – Petr Konecny
    Nov 11 at 15:12










  • What is the allowable range of x,y? Can x or y be negative?
    – chux
    Nov 11 at 16:13








  • 2




    en.wikipedia.org/wiki/Diophantine_equation
    – Matt Timmermans
    Nov 11 at 16:34






  • 1




    @Petr Konecny "it takes way too long" --> This can be solved without any iterations on x,y, yet looks like you have an acceptable solution already - too bad.
    – chux
    Nov 11 at 19:11













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am counting the value of possible combinations of x and y. It works but when I put big numbers it takes way too long. Do you have any ideas for better algorithm?



ax + by = c



The input of program is a, b and c, which should be non-negative numbers.
My code looks like this:



int combs=0;
for(int x=0; x < c; x++) {
for(int y=0; y < c; y++) {
if( (a*x) + (b*y) == c) {
combs++;
}
}
}









share|improve this question















I am counting the value of possible combinations of x and y. It works but when I put big numbers it takes way too long. Do you have any ideas for better algorithm?



ax + by = c



The input of program is a, b and c, which should be non-negative numbers.
My code looks like this:



int combs=0;
for(int x=0; x < c; x++) {
for(int y=0; y < c; y++) {
if( (a*x) + (b*y) == c) {
combs++;
}
}
}






c algorithm equation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 15:16









Broman

6,190112241




6,190112241










asked Nov 11 at 15:09









Petr Konecny

64




64












  • Are there limits on a, b, and c?
    – pstrjds
    Nov 11 at 15:11










  • They have to be bigger or equal to 0, thats all.
    – Petr Konecny
    Nov 11 at 15:12










  • What is the allowable range of x,y? Can x or y be negative?
    – chux
    Nov 11 at 16:13








  • 2




    en.wikipedia.org/wiki/Diophantine_equation
    – Matt Timmermans
    Nov 11 at 16:34






  • 1




    @Petr Konecny "it takes way too long" --> This can be solved without any iterations on x,y, yet looks like you have an acceptable solution already - too bad.
    – chux
    Nov 11 at 19:11


















  • Are there limits on a, b, and c?
    – pstrjds
    Nov 11 at 15:11










  • They have to be bigger or equal to 0, thats all.
    – Petr Konecny
    Nov 11 at 15:12










  • What is the allowable range of x,y? Can x or y be negative?
    – chux
    Nov 11 at 16:13








  • 2




    en.wikipedia.org/wiki/Diophantine_equation
    – Matt Timmermans
    Nov 11 at 16:34






  • 1




    @Petr Konecny "it takes way too long" --> This can be solved without any iterations on x,y, yet looks like you have an acceptable solution already - too bad.
    – chux
    Nov 11 at 19:11
















Are there limits on a, b, and c?
– pstrjds
Nov 11 at 15:11




Are there limits on a, b, and c?
– pstrjds
Nov 11 at 15:11












They have to be bigger or equal to 0, thats all.
– Petr Konecny
Nov 11 at 15:12




They have to be bigger or equal to 0, thats all.
– Petr Konecny
Nov 11 at 15:12












What is the allowable range of x,y? Can x or y be negative?
– chux
Nov 11 at 16:13






What is the allowable range of x,y? Can x or y be negative?
– chux
Nov 11 at 16:13






2




2




en.wikipedia.org/wiki/Diophantine_equation
– Matt Timmermans
Nov 11 at 16:34




en.wikipedia.org/wiki/Diophantine_equation
– Matt Timmermans
Nov 11 at 16:34




1




1




@Petr Konecny "it takes way too long" --> This can be solved without any iterations on x,y, yet looks like you have an acceptable solution already - too bad.
– chux
Nov 11 at 19:11




@Petr Konecny "it takes way too long" --> This can be solved without any iterations on x,y, yet looks like you have an acceptable solution already - too bad.
– chux
Nov 11 at 19:11












1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










A much faster way is to do some math first. ax+by=c => y=(c-ax)/b



int combs=0;
for(int x=0; x < c; x++) {
int y = (c-a*x)/b;
if( (a*x) + (b*y) == c)
combs++;
}


Getting rid of that nested loop is the most important detail to improve performance. Another thing you could do is to do as Antti Haapala suggested in comments below and use ax instead of x.



int combs=0;
for(int ax=0; ax < c; ax+=a) {
int y = (c-ax)/b;
if( (ax) + (b*y) == c)
combs++;
}





share|improve this answer























  • Lazy thinking today, but isn't it enough to check for zero remainder?
    – Antti Haapala
    Nov 11 at 15:40










  • @AnttiHaapala Quite possible. I don't know.
    – Broman
    Nov 11 at 16:00










  • also you don't need to loop until c but ceil(c / a). Or use ax as the variable, and increase by a :)
    – Antti Haapala
    Nov 11 at 16:03












  • What do you mean by zero reminder? :)
    – Petr Konecny
    Nov 11 at 16:16










  • This will counts as solutions y < 0, yet not x < 0. Suggest x < c --> x < c/a to prevent that - faster too.
    – chux
    Nov 11 at 19:09











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%2f53250060%2fthe-fastest-algorithm-to-solve-equation-with-2-unknown-parametrs-in-c%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
5
down vote



accepted










A much faster way is to do some math first. ax+by=c => y=(c-ax)/b



int combs=0;
for(int x=0; x < c; x++) {
int y = (c-a*x)/b;
if( (a*x) + (b*y) == c)
combs++;
}


Getting rid of that nested loop is the most important detail to improve performance. Another thing you could do is to do as Antti Haapala suggested in comments below and use ax instead of x.



int combs=0;
for(int ax=0; ax < c; ax+=a) {
int y = (c-ax)/b;
if( (ax) + (b*y) == c)
combs++;
}





share|improve this answer























  • Lazy thinking today, but isn't it enough to check for zero remainder?
    – Antti Haapala
    Nov 11 at 15:40










  • @AnttiHaapala Quite possible. I don't know.
    – Broman
    Nov 11 at 16:00










  • also you don't need to loop until c but ceil(c / a). Or use ax as the variable, and increase by a :)
    – Antti Haapala
    Nov 11 at 16:03












  • What do you mean by zero reminder? :)
    – Petr Konecny
    Nov 11 at 16:16










  • This will counts as solutions y < 0, yet not x < 0. Suggest x < c --> x < c/a to prevent that - faster too.
    – chux
    Nov 11 at 19:09















up vote
5
down vote



accepted










A much faster way is to do some math first. ax+by=c => y=(c-ax)/b



int combs=0;
for(int x=0; x < c; x++) {
int y = (c-a*x)/b;
if( (a*x) + (b*y) == c)
combs++;
}


Getting rid of that nested loop is the most important detail to improve performance. Another thing you could do is to do as Antti Haapala suggested in comments below and use ax instead of x.



int combs=0;
for(int ax=0; ax < c; ax+=a) {
int y = (c-ax)/b;
if( (ax) + (b*y) == c)
combs++;
}





share|improve this answer























  • Lazy thinking today, but isn't it enough to check for zero remainder?
    – Antti Haapala
    Nov 11 at 15:40










  • @AnttiHaapala Quite possible. I don't know.
    – Broman
    Nov 11 at 16:00










  • also you don't need to loop until c but ceil(c / a). Or use ax as the variable, and increase by a :)
    – Antti Haapala
    Nov 11 at 16:03












  • What do you mean by zero reminder? :)
    – Petr Konecny
    Nov 11 at 16:16










  • This will counts as solutions y < 0, yet not x < 0. Suggest x < c --> x < c/a to prevent that - faster too.
    – chux
    Nov 11 at 19:09













up vote
5
down vote



accepted







up vote
5
down vote



accepted






A much faster way is to do some math first. ax+by=c => y=(c-ax)/b



int combs=0;
for(int x=0; x < c; x++) {
int y = (c-a*x)/b;
if( (a*x) + (b*y) == c)
combs++;
}


Getting rid of that nested loop is the most important detail to improve performance. Another thing you could do is to do as Antti Haapala suggested in comments below and use ax instead of x.



int combs=0;
for(int ax=0; ax < c; ax+=a) {
int y = (c-ax)/b;
if( (ax) + (b*y) == c)
combs++;
}





share|improve this answer














A much faster way is to do some math first. ax+by=c => y=(c-ax)/b



int combs=0;
for(int x=0; x < c; x++) {
int y = (c-a*x)/b;
if( (a*x) + (b*y) == c)
combs++;
}


Getting rid of that nested loop is the most important detail to improve performance. Another thing you could do is to do as Antti Haapala suggested in comments below and use ax instead of x.



int combs=0;
for(int ax=0; ax < c; ax+=a) {
int y = (c-ax)/b;
if( (ax) + (b*y) == c)
combs++;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 19:16

























answered Nov 11 at 15:15









Broman

6,190112241




6,190112241












  • Lazy thinking today, but isn't it enough to check for zero remainder?
    – Antti Haapala
    Nov 11 at 15:40










  • @AnttiHaapala Quite possible. I don't know.
    – Broman
    Nov 11 at 16:00










  • also you don't need to loop until c but ceil(c / a). Or use ax as the variable, and increase by a :)
    – Antti Haapala
    Nov 11 at 16:03












  • What do you mean by zero reminder? :)
    – Petr Konecny
    Nov 11 at 16:16










  • This will counts as solutions y < 0, yet not x < 0. Suggest x < c --> x < c/a to prevent that - faster too.
    – chux
    Nov 11 at 19:09


















  • Lazy thinking today, but isn't it enough to check for zero remainder?
    – Antti Haapala
    Nov 11 at 15:40










  • @AnttiHaapala Quite possible. I don't know.
    – Broman
    Nov 11 at 16:00










  • also you don't need to loop until c but ceil(c / a). Or use ax as the variable, and increase by a :)
    – Antti Haapala
    Nov 11 at 16:03












  • What do you mean by zero reminder? :)
    – Petr Konecny
    Nov 11 at 16:16










  • This will counts as solutions y < 0, yet not x < 0. Suggest x < c --> x < c/a to prevent that - faster too.
    – chux
    Nov 11 at 19:09
















Lazy thinking today, but isn't it enough to check for zero remainder?
– Antti Haapala
Nov 11 at 15:40




Lazy thinking today, but isn't it enough to check for zero remainder?
– Antti Haapala
Nov 11 at 15:40












@AnttiHaapala Quite possible. I don't know.
– Broman
Nov 11 at 16:00




@AnttiHaapala Quite possible. I don't know.
– Broman
Nov 11 at 16:00












also you don't need to loop until c but ceil(c / a). Or use ax as the variable, and increase by a :)
– Antti Haapala
Nov 11 at 16:03






also you don't need to loop until c but ceil(c / a). Or use ax as the variable, and increase by a :)
– Antti Haapala
Nov 11 at 16:03














What do you mean by zero reminder? :)
– Petr Konecny
Nov 11 at 16:16




What do you mean by zero reminder? :)
– Petr Konecny
Nov 11 at 16:16












This will counts as solutions y < 0, yet not x < 0. Suggest x < c --> x < c/a to prevent that - faster too.
– chux
Nov 11 at 19:09




This will counts as solutions y < 0, yet not x < 0. Suggest x < c --> x < c/a to prevent that - faster too.
– chux
Nov 11 at 19:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250060%2fthe-fastest-algorithm-to-solve-equation-with-2-unknown-parametrs-in-c%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python