Better way to solve multi variable polynomial?
$begingroup$
$$
a + b^2 + c^3 + d^4 le S
$$
For above equation, I need to count possible solutions.
I tried following approach.
Integer inp=Integer.parseInt(br.readLine());
// get maximum possible value of b,c,d which satisfy equation
int b=(int) Math.sqrt(inp);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(inp);
int count=0;
for (int i = 0; i <= inp; i++) {
for (int j = 0; j <= b; j++) {
for (int j2 = 0; j2 <= c; j2++) {
for (int k = 0; k <= d; k++) {
int total=i+j*j+(int)Math.pow(j2, 3)+(int)Math.pow(k,4);
if(total<=inp) {
count++;
}
}
}
}
}
Time complexity: $O(n times sqrt{n} times sqrt[3]{n} times sqrt[4]{n})$ (Not sure)
For large value, this takes time. Can someone please help me with better solution with time complexity?
java algorithm interview-questions
$endgroup$
add a comment |
$begingroup$
$$
a + b^2 + c^3 + d^4 le S
$$
For above equation, I need to count possible solutions.
I tried following approach.
Integer inp=Integer.parseInt(br.readLine());
// get maximum possible value of b,c,d which satisfy equation
int b=(int) Math.sqrt(inp);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(inp);
int count=0;
for (int i = 0; i <= inp; i++) {
for (int j = 0; j <= b; j++) {
for (int j2 = 0; j2 <= c; j2++) {
for (int k = 0; k <= d; k++) {
int total=i+j*j+(int)Math.pow(j2, 3)+(int)Math.pow(k,4);
if(total<=inp) {
count++;
}
}
}
}
}
Time complexity: $O(n times sqrt{n} times sqrt[3]{n} times sqrt[4]{n})$ (Not sure)
For large value, this takes time. Can someone please help me with better solution with time complexity?
java algorithm interview-questions
$endgroup$
1
$begingroup$
Area,b,c,delements of the natural numbers ? Can they be negative, or even complex? Please update the problem statement :)
$endgroup$
– RobAu
17 hours ago
add a comment |
$begingroup$
$$
a + b^2 + c^3 + d^4 le S
$$
For above equation, I need to count possible solutions.
I tried following approach.
Integer inp=Integer.parseInt(br.readLine());
// get maximum possible value of b,c,d which satisfy equation
int b=(int) Math.sqrt(inp);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(inp);
int count=0;
for (int i = 0; i <= inp; i++) {
for (int j = 0; j <= b; j++) {
for (int j2 = 0; j2 <= c; j2++) {
for (int k = 0; k <= d; k++) {
int total=i+j*j+(int)Math.pow(j2, 3)+(int)Math.pow(k,4);
if(total<=inp) {
count++;
}
}
}
}
}
Time complexity: $O(n times sqrt{n} times sqrt[3]{n} times sqrt[4]{n})$ (Not sure)
For large value, this takes time. Can someone please help me with better solution with time complexity?
java algorithm interview-questions
$endgroup$
$$
a + b^2 + c^3 + d^4 le S
$$
For above equation, I need to count possible solutions.
I tried following approach.
Integer inp=Integer.parseInt(br.readLine());
// get maximum possible value of b,c,d which satisfy equation
int b=(int) Math.sqrt(inp);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(inp);
int count=0;
for (int i = 0; i <= inp; i++) {
for (int j = 0; j <= b; j++) {
for (int j2 = 0; j2 <= c; j2++) {
for (int k = 0; k <= d; k++) {
int total=i+j*j+(int)Math.pow(j2, 3)+(int)Math.pow(k,4);
if(total<=inp) {
count++;
}
}
}
}
}
Time complexity: $O(n times sqrt{n} times sqrt[3]{n} times sqrt[4]{n})$ (Not sure)
For large value, this takes time. Can someone please help me with better solution with time complexity?
java algorithm interview-questions
java algorithm interview-questions
edited 17 hours ago
Peter Taylor
15.9k2759
15.9k2759
asked 17 hours ago
user3857802user3857802
211
211
1
$begingroup$
Area,b,c,delements of the natural numbers ? Can they be negative, or even complex? Please update the problem statement :)
$endgroup$
– RobAu
17 hours ago
add a comment |
1
$begingroup$
Area,b,c,delements of the natural numbers ? Can they be negative, or even complex? Please update the problem statement :)
$endgroup$
– RobAu
17 hours ago
1
1
$begingroup$
Are
a,b,c,d elements of the natural numbers ? Can they be negative, or even complex? Please update the problem statement :)$endgroup$
– RobAu
17 hours ago
$begingroup$
Are
a,b,c,d elements of the natural numbers ? Can they be negative, or even complex? Please update the problem statement :)$endgroup$
– RobAu
17 hours ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
a + b^2 + c^3 + d^4 <= S
For above equation ,I need to count possible solutions.
I tried following approach.
Integer inp=Integer.parseInt(br.readLine());
// get maximum possible value of b,c,d which satisfy equation
int b=(int) Math.sqrt(inp);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(inp);
int count=0;
...
If I were the interviewer, I would already have marked you down heavily for your choice of variable names. The problem statement defines a, b, c, d, S. The natural choice of variable names for those values are a, b, c, d, S. Using b, c, d for something else almost looks as though you were deliberately trying to make the code unmaintainable.
if(total<=inp) {
count++;
}
That is nearly always the wrong way to count something.
If I asked you to count solutions to $a le S$ would you write a loop? I hope not, because you can do it without any loop or any mathematical operations.
Now, how about $a + b^2 le S$? This can be done with one mathematical operation.
Cubes already grow quite fast, so
for (int c = 0; c <= maxC; c++) {
for (int d = 0; d <= maxD; d++) {
count += countAB(S - c*c*c - d*d*d*d)
}
}
would be reasonably efficient. If you really want to microoptimise you can eliminate the multiplications in favour of some accumulators and addition, but that's probably not necessary for an interview question. They might ask about it in a follow-up.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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: "196"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
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%2fcodereview.stackexchange.com%2fquestions%2f211526%2fbetter-way-to-solve-multi-variable-polynomial%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
$begingroup$
a + b^2 + c^3 + d^4 <= S
For above equation ,I need to count possible solutions.
I tried following approach.
Integer inp=Integer.parseInt(br.readLine());
// get maximum possible value of b,c,d which satisfy equation
int b=(int) Math.sqrt(inp);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(inp);
int count=0;
...
If I were the interviewer, I would already have marked you down heavily for your choice of variable names. The problem statement defines a, b, c, d, S. The natural choice of variable names for those values are a, b, c, d, S. Using b, c, d for something else almost looks as though you were deliberately trying to make the code unmaintainable.
if(total<=inp) {
count++;
}
That is nearly always the wrong way to count something.
If I asked you to count solutions to $a le S$ would you write a loop? I hope not, because you can do it without any loop or any mathematical operations.
Now, how about $a + b^2 le S$? This can be done with one mathematical operation.
Cubes already grow quite fast, so
for (int c = 0; c <= maxC; c++) {
for (int d = 0; d <= maxD; d++) {
count += countAB(S - c*c*c - d*d*d*d)
}
}
would be reasonably efficient. If you really want to microoptimise you can eliminate the multiplications in favour of some accumulators and addition, but that's probably not necessary for an interview question. They might ask about it in a follow-up.
$endgroup$
add a comment |
$begingroup$
a + b^2 + c^3 + d^4 <= S
For above equation ,I need to count possible solutions.
I tried following approach.
Integer inp=Integer.parseInt(br.readLine());
// get maximum possible value of b,c,d which satisfy equation
int b=(int) Math.sqrt(inp);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(inp);
int count=0;
...
If I were the interviewer, I would already have marked you down heavily for your choice of variable names. The problem statement defines a, b, c, d, S. The natural choice of variable names for those values are a, b, c, d, S. Using b, c, d for something else almost looks as though you were deliberately trying to make the code unmaintainable.
if(total<=inp) {
count++;
}
That is nearly always the wrong way to count something.
If I asked you to count solutions to $a le S$ would you write a loop? I hope not, because you can do it without any loop or any mathematical operations.
Now, how about $a + b^2 le S$? This can be done with one mathematical operation.
Cubes already grow quite fast, so
for (int c = 0; c <= maxC; c++) {
for (int d = 0; d <= maxD; d++) {
count += countAB(S - c*c*c - d*d*d*d)
}
}
would be reasonably efficient. If you really want to microoptimise you can eliminate the multiplications in favour of some accumulators and addition, but that's probably not necessary for an interview question. They might ask about it in a follow-up.
$endgroup$
add a comment |
$begingroup$
a + b^2 + c^3 + d^4 <= S
For above equation ,I need to count possible solutions.
I tried following approach.
Integer inp=Integer.parseInt(br.readLine());
// get maximum possible value of b,c,d which satisfy equation
int b=(int) Math.sqrt(inp);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(inp);
int count=0;
...
If I were the interviewer, I would already have marked you down heavily for your choice of variable names. The problem statement defines a, b, c, d, S. The natural choice of variable names for those values are a, b, c, d, S. Using b, c, d for something else almost looks as though you were deliberately trying to make the code unmaintainable.
if(total<=inp) {
count++;
}
That is nearly always the wrong way to count something.
If I asked you to count solutions to $a le S$ would you write a loop? I hope not, because you can do it without any loop or any mathematical operations.
Now, how about $a + b^2 le S$? This can be done with one mathematical operation.
Cubes already grow quite fast, so
for (int c = 0; c <= maxC; c++) {
for (int d = 0; d <= maxD; d++) {
count += countAB(S - c*c*c - d*d*d*d)
}
}
would be reasonably efficient. If you really want to microoptimise you can eliminate the multiplications in favour of some accumulators and addition, but that's probably not necessary for an interview question. They might ask about it in a follow-up.
$endgroup$
a + b^2 + c^3 + d^4 <= S
For above equation ,I need to count possible solutions.
I tried following approach.
Integer inp=Integer.parseInt(br.readLine());
// get maximum possible value of b,c,d which satisfy equation
int b=(int) Math.sqrt(inp);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(inp);
int count=0;
...
If I were the interviewer, I would already have marked you down heavily for your choice of variable names. The problem statement defines a, b, c, d, S. The natural choice of variable names for those values are a, b, c, d, S. Using b, c, d for something else almost looks as though you were deliberately trying to make the code unmaintainable.
if(total<=inp) {
count++;
}
That is nearly always the wrong way to count something.
If I asked you to count solutions to $a le S$ would you write a loop? I hope not, because you can do it without any loop or any mathematical operations.
Now, how about $a + b^2 le S$? This can be done with one mathematical operation.
Cubes already grow quite fast, so
for (int c = 0; c <= maxC; c++) {
for (int d = 0; d <= maxD; d++) {
count += countAB(S - c*c*c - d*d*d*d)
}
}
would be reasonably efficient. If you really want to microoptimise you can eliminate the multiplications in favour of some accumulators and addition, but that's probably not necessary for an interview question. They might ask about it in a follow-up.
answered 17 hours ago
Peter TaylorPeter Taylor
15.9k2759
15.9k2759
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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%2fcodereview.stackexchange.com%2fquestions%2f211526%2fbetter-way-to-solve-multi-variable-polynomial%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
1
$begingroup$
Are
a,b,c,delements of the natural numbers ? Can they be negative, or even complex? Please update the problem statement :)$endgroup$
– RobAu
17 hours ago