Find the minimum number of steps to decrease N to zero
I'm facing some difficulties in the last few days while trying to finish the following task, I hope you guys can assist :
I'm given a single number N, and I'm allowed to perform any of the two operations on N in each move :
One - If we take 2 integers where N = x * y , then we can change the value of N to the maximum between x and y.
Two - Decrease the value of N by 1.
I want to find the minimum number of steps to reduce N to zero.
This is what I have so far, I'm not sure what is the best way to implement the function to find the divisor (someFindDevisorFunction), and if this 'f' function would actually produce the required output.
int f(int n)
{
int div,firstWay,secondWay;
if(n == 0)
return 0;
div = SomefindDivisorFunction(n);
firstWay = 1 + f(n-1);
if(div != 1)
{
secondWay = 1 + f(div);
if (firstWay < secondWay)
return firstWay;
return secondWay;
}
return firstWay;
}
For example, if I enter the number 150 , the output would be :
75 - 25 - 5 - 4 - 2 - 1 - 0
c recursion minimum
|
show 8 more comments
I'm facing some difficulties in the last few days while trying to finish the following task, I hope you guys can assist :
I'm given a single number N, and I'm allowed to perform any of the two operations on N in each move :
One - If we take 2 integers where N = x * y , then we can change the value of N to the maximum between x and y.
Two - Decrease the value of N by 1.
I want to find the minimum number of steps to reduce N to zero.
This is what I have so far, I'm not sure what is the best way to implement the function to find the divisor (someFindDevisorFunction), and if this 'f' function would actually produce the required output.
int f(int n)
{
int div,firstWay,secondWay;
if(n == 0)
return 0;
div = SomefindDivisorFunction(n);
firstWay = 1 + f(n-1);
if(div != 1)
{
secondWay = 1 + f(div);
if (firstWay < secondWay)
return firstWay;
return secondWay;
}
return firstWay;
}
For example, if I enter the number 150 , the output would be :
75 - 25 - 5 - 4 - 2 - 1 - 0
c recursion minimum
3
I don't understand: for 150 I would say: 150=15*10 => 15. 15=5*3=> 5, ..., which is one step less than your proposed solution.
– Dominique
Nov 25 '18 at 20:36
1
I'm not grasping why your output has 75 as the first step for 150. Why not 15, since 150 = 15 * 10 and 15 = max(15, 10)? It looks to me like you'll get the most rapid convergence to zero by picking a and b to be the integer cofactors that are closest to sqrt(N).
– pjs
Nov 25 '18 at 20:36
1
What is your question? A different approach than the simple brute force you have shown? The code forSomefindDivisorFunction()? Please be more specific. If it is the code for the function, then consider focusing your question on that part,
– Yunnosch
Nov 25 '18 at 20:36
Did you forget a piece of the question? for example, something about whether a and b are relatively prime? otherwise, as suggested here, you should factor it so that the two factors are as close as possible to sqrt(N).
– CoffeeTableEspresso
Nov 25 '18 at 21:00
1
As the problem is stated in the question, for any positive N, we have N → −1 (since N = −1•−N and −1 = max(−1, −N)) → −2 (by decrement) → 2 (since −2 = −1•2) → 1 (by decrement) → 0 (by decrement).
– Eric Postpischil
Nov 26 '18 at 2:08
|
show 8 more comments
I'm facing some difficulties in the last few days while trying to finish the following task, I hope you guys can assist :
I'm given a single number N, and I'm allowed to perform any of the two operations on N in each move :
One - If we take 2 integers where N = x * y , then we can change the value of N to the maximum between x and y.
Two - Decrease the value of N by 1.
I want to find the minimum number of steps to reduce N to zero.
This is what I have so far, I'm not sure what is the best way to implement the function to find the divisor (someFindDevisorFunction), and if this 'f' function would actually produce the required output.
int f(int n)
{
int div,firstWay,secondWay;
if(n == 0)
return 0;
div = SomefindDivisorFunction(n);
firstWay = 1 + f(n-1);
if(div != 1)
{
secondWay = 1 + f(div);
if (firstWay < secondWay)
return firstWay;
return secondWay;
}
return firstWay;
}
For example, if I enter the number 150 , the output would be :
75 - 25 - 5 - 4 - 2 - 1 - 0
c recursion minimum
I'm facing some difficulties in the last few days while trying to finish the following task, I hope you guys can assist :
I'm given a single number N, and I'm allowed to perform any of the two operations on N in each move :
One - If we take 2 integers where N = x * y , then we can change the value of N to the maximum between x and y.
Two - Decrease the value of N by 1.
I want to find the minimum number of steps to reduce N to zero.
This is what I have so far, I'm not sure what is the best way to implement the function to find the divisor (someFindDevisorFunction), and if this 'f' function would actually produce the required output.
int f(int n)
{
int div,firstWay,secondWay;
if(n == 0)
return 0;
div = SomefindDivisorFunction(n);
firstWay = 1 + f(n-1);
if(div != 1)
{
secondWay = 1 + f(div);
if (firstWay < secondWay)
return firstWay;
return secondWay;
}
return firstWay;
}
For example, if I enter the number 150 , the output would be :
75 - 25 - 5 - 4 - 2 - 1 - 0
c recursion minimum
c recursion minimum
edited Nov 26 '18 at 7:01
אברגיל יעקובו
asked Nov 25 '18 at 20:26
אברגיל יעקובואברגיל יעקובו
716
716
3
I don't understand: for 150 I would say: 150=15*10 => 15. 15=5*3=> 5, ..., which is one step less than your proposed solution.
– Dominique
Nov 25 '18 at 20:36
1
I'm not grasping why your output has 75 as the first step for 150. Why not 15, since 150 = 15 * 10 and 15 = max(15, 10)? It looks to me like you'll get the most rapid convergence to zero by picking a and b to be the integer cofactors that are closest to sqrt(N).
– pjs
Nov 25 '18 at 20:36
1
What is your question? A different approach than the simple brute force you have shown? The code forSomefindDivisorFunction()? Please be more specific. If it is the code for the function, then consider focusing your question on that part,
– Yunnosch
Nov 25 '18 at 20:36
Did you forget a piece of the question? for example, something about whether a and b are relatively prime? otherwise, as suggested here, you should factor it so that the two factors are as close as possible to sqrt(N).
– CoffeeTableEspresso
Nov 25 '18 at 21:00
1
As the problem is stated in the question, for any positive N, we have N → −1 (since N = −1•−N and −1 = max(−1, −N)) → −2 (by decrement) → 2 (since −2 = −1•2) → 1 (by decrement) → 0 (by decrement).
– Eric Postpischil
Nov 26 '18 at 2:08
|
show 8 more comments
3
I don't understand: for 150 I would say: 150=15*10 => 15. 15=5*3=> 5, ..., which is one step less than your proposed solution.
– Dominique
Nov 25 '18 at 20:36
1
I'm not grasping why your output has 75 as the first step for 150. Why not 15, since 150 = 15 * 10 and 15 = max(15, 10)? It looks to me like you'll get the most rapid convergence to zero by picking a and b to be the integer cofactors that are closest to sqrt(N).
– pjs
Nov 25 '18 at 20:36
1
What is your question? A different approach than the simple brute force you have shown? The code forSomefindDivisorFunction()? Please be more specific. If it is the code for the function, then consider focusing your question on that part,
– Yunnosch
Nov 25 '18 at 20:36
Did you forget a piece of the question? for example, something about whether a and b are relatively prime? otherwise, as suggested here, you should factor it so that the two factors are as close as possible to sqrt(N).
– CoffeeTableEspresso
Nov 25 '18 at 21:00
1
As the problem is stated in the question, for any positive N, we have N → −1 (since N = −1•−N and −1 = max(−1, −N)) → −2 (by decrement) → 2 (since −2 = −1•2) → 1 (by decrement) → 0 (by decrement).
– Eric Postpischil
Nov 26 '18 at 2:08
3
3
I don't understand: for 150 I would say: 150=15*10 => 15. 15=5*3=> 5, ..., which is one step less than your proposed solution.
– Dominique
Nov 25 '18 at 20:36
I don't understand: for 150 I would say: 150=15*10 => 15. 15=5*3=> 5, ..., which is one step less than your proposed solution.
– Dominique
Nov 25 '18 at 20:36
1
1
I'm not grasping why your output has 75 as the first step for 150. Why not 15, since 150 = 15 * 10 and 15 = max(15, 10)? It looks to me like you'll get the most rapid convergence to zero by picking a and b to be the integer cofactors that are closest to sqrt(N).
– pjs
Nov 25 '18 at 20:36
I'm not grasping why your output has 75 as the first step for 150. Why not 15, since 150 = 15 * 10 and 15 = max(15, 10)? It looks to me like you'll get the most rapid convergence to zero by picking a and b to be the integer cofactors that are closest to sqrt(N).
– pjs
Nov 25 '18 at 20:36
1
1
What is your question? A different approach than the simple brute force you have shown? The code for
SomefindDivisorFunction()? Please be more specific. If it is the code for the function, then consider focusing your question on that part,– Yunnosch
Nov 25 '18 at 20:36
What is your question? A different approach than the simple brute force you have shown? The code for
SomefindDivisorFunction()? Please be more specific. If it is the code for the function, then consider focusing your question on that part,– Yunnosch
Nov 25 '18 at 20:36
Did you forget a piece of the question? for example, something about whether a and b are relatively prime? otherwise, as suggested here, you should factor it so that the two factors are as close as possible to sqrt(N).
– CoffeeTableEspresso
Nov 25 '18 at 21:00
Did you forget a piece of the question? for example, something about whether a and b are relatively prime? otherwise, as suggested here, you should factor it so that the two factors are as close as possible to sqrt(N).
– CoffeeTableEspresso
Nov 25 '18 at 21:00
1
1
As the problem is stated in the question, for any positive N, we have N → −1 (since N = −1•−N and −1 = max(−1, −N)) → −2 (by decrement) → 2 (since −2 = −1•2) → 1 (by decrement) → 0 (by decrement).
– Eric Postpischil
Nov 26 '18 at 2:08
As the problem is stated in the question, for any positive N, we have N → −1 (since N = −1•−N and −1 = max(−1, −N)) → −2 (by decrement) → 2 (since −2 = −1•2) → 1 (by decrement) → 0 (by decrement).
– Eric Postpischil
Nov 26 '18 at 2:08
|
show 8 more comments
3 Answers
3
active
oldest
votes
I see this a recursive or iterative problem.
OP's approach hints at recursive.
A recursive solution follows:
At each step, code counts the steps of the various alternatives:
steps(n) = min(
steps(factor1_of_n) + 1,
steps(factor2_of_n) + 1,
steps(factor3_of_n) + 1,
...
steps(n-1) + 1)
The coded solution below is inefficient, but it does explore all possibilities and gets to the answer.
int solve_helper(int n, bool print) {
int best_quot = 0;
int best_quot_score = INT_MAX;
int quot;
for (int p = 2; p <= (quot = n / p); p++) {
int rem = n % p;
if (rem == 0 && quot > 1) {
int score = solve_helper(quot, false) + 1;
if (score < best_quot_score) {
best_quot_score = score;
best_quot = quot;
}
}
}
int dec_score = n > 0 ? solve_helper(n - 1, false) + 1 : 0;
if (best_quot_score < dec_score) {
if (print) {
printf("/ %d ", best_quot);
solve_helper(best_quot, true);
}
return best_quot_score;
}
if (print && n > 0) {
printf("- %d ", n - 1);
solve_helper(n - 1, true);
}
return dec_score;
}
int main() {
int n = 75;
printf("%d ", n);
solve(n, true);
printf("n");
}
Output
75 / 25 / 5 - 4 / 2 - 1 - 0
Iterative
TBD
Thank you for your answer, I'll test it when I reach home. However, I want to try and find how to do it more efficient, in a recursive way.
– אברגיל יעקובו
Nov 26 '18 at 7:03
Remember to make it function right, before efficiency. It is easy to make faster, yet not arrive at the optimal solution.
– chux
Nov 26 '18 at 7:18
add a comment |
For n > 1 you have to test for all divisors between sqrt(n) and n - 1 (inclusive), not simply the largest.
Here is a non-recursive solution:
#include <stdio.h>
int main ()
{
// assume N non-negative
int N = 150;
int next[N + 1];
int steps[N + 1];
steps[0] = 0;
for (int n = 1; n <= N; n++) {
next[n] = n - 1;
steps[n] = 1 + steps[n-1];
for (int i = n - 1; i * i >= n; i--) {
if (n % i == 0) {
int tmp = 1 + steps[i];
if (tmp < steps[n]) {
steps[n] = tmp;
next[n] = i;
}
}
}
}
printf ("%d: %d stepsn", N, steps[N]);
printf ("%d", N);
int tmp = N;
while (tmp != 0) {
tmp = next[tmp];
printf (" -> %d", tmp);
}
printf ("n");
}
add a comment |
If you start looking for a divisor with 2, and work your way up, then the last pair of divisors you find will include the largest divisor. Alternatively you can start searching with divisor = N/2 and work down, when the first divisor found will have be largest divisor of N.
True that working ones way or or down will find the largest divisor, yet it is unclear that when considering step 1, finding the largest divisor in each step will result in the fewest steps. Perhaps using a not-the-largest- divisors at an early stage results in the fewest steps?
– chux
Nov 25 '18 at 23:45
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53471588%2ffind-the-minimum-number-of-steps-to-decrease-n-to-zero%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I see this a recursive or iterative problem.
OP's approach hints at recursive.
A recursive solution follows:
At each step, code counts the steps of the various alternatives:
steps(n) = min(
steps(factor1_of_n) + 1,
steps(factor2_of_n) + 1,
steps(factor3_of_n) + 1,
...
steps(n-1) + 1)
The coded solution below is inefficient, but it does explore all possibilities and gets to the answer.
int solve_helper(int n, bool print) {
int best_quot = 0;
int best_quot_score = INT_MAX;
int quot;
for (int p = 2; p <= (quot = n / p); p++) {
int rem = n % p;
if (rem == 0 && quot > 1) {
int score = solve_helper(quot, false) + 1;
if (score < best_quot_score) {
best_quot_score = score;
best_quot = quot;
}
}
}
int dec_score = n > 0 ? solve_helper(n - 1, false) + 1 : 0;
if (best_quot_score < dec_score) {
if (print) {
printf("/ %d ", best_quot);
solve_helper(best_quot, true);
}
return best_quot_score;
}
if (print && n > 0) {
printf("- %d ", n - 1);
solve_helper(n - 1, true);
}
return dec_score;
}
int main() {
int n = 75;
printf("%d ", n);
solve(n, true);
printf("n");
}
Output
75 / 25 / 5 - 4 / 2 - 1 - 0
Iterative
TBD
Thank you for your answer, I'll test it when I reach home. However, I want to try and find how to do it more efficient, in a recursive way.
– אברגיל יעקובו
Nov 26 '18 at 7:03
Remember to make it function right, before efficiency. It is easy to make faster, yet not arrive at the optimal solution.
– chux
Nov 26 '18 at 7:18
add a comment |
I see this a recursive or iterative problem.
OP's approach hints at recursive.
A recursive solution follows:
At each step, code counts the steps of the various alternatives:
steps(n) = min(
steps(factor1_of_n) + 1,
steps(factor2_of_n) + 1,
steps(factor3_of_n) + 1,
...
steps(n-1) + 1)
The coded solution below is inefficient, but it does explore all possibilities and gets to the answer.
int solve_helper(int n, bool print) {
int best_quot = 0;
int best_quot_score = INT_MAX;
int quot;
for (int p = 2; p <= (quot = n / p); p++) {
int rem = n % p;
if (rem == 0 && quot > 1) {
int score = solve_helper(quot, false) + 1;
if (score < best_quot_score) {
best_quot_score = score;
best_quot = quot;
}
}
}
int dec_score = n > 0 ? solve_helper(n - 1, false) + 1 : 0;
if (best_quot_score < dec_score) {
if (print) {
printf("/ %d ", best_quot);
solve_helper(best_quot, true);
}
return best_quot_score;
}
if (print && n > 0) {
printf("- %d ", n - 1);
solve_helper(n - 1, true);
}
return dec_score;
}
int main() {
int n = 75;
printf("%d ", n);
solve(n, true);
printf("n");
}
Output
75 / 25 / 5 - 4 / 2 - 1 - 0
Iterative
TBD
Thank you for your answer, I'll test it when I reach home. However, I want to try and find how to do it more efficient, in a recursive way.
– אברגיל יעקובו
Nov 26 '18 at 7:03
Remember to make it function right, before efficiency. It is easy to make faster, yet not arrive at the optimal solution.
– chux
Nov 26 '18 at 7:18
add a comment |
I see this a recursive or iterative problem.
OP's approach hints at recursive.
A recursive solution follows:
At each step, code counts the steps of the various alternatives:
steps(n) = min(
steps(factor1_of_n) + 1,
steps(factor2_of_n) + 1,
steps(factor3_of_n) + 1,
...
steps(n-1) + 1)
The coded solution below is inefficient, but it does explore all possibilities and gets to the answer.
int solve_helper(int n, bool print) {
int best_quot = 0;
int best_quot_score = INT_MAX;
int quot;
for (int p = 2; p <= (quot = n / p); p++) {
int rem = n % p;
if (rem == 0 && quot > 1) {
int score = solve_helper(quot, false) + 1;
if (score < best_quot_score) {
best_quot_score = score;
best_quot = quot;
}
}
}
int dec_score = n > 0 ? solve_helper(n - 1, false) + 1 : 0;
if (best_quot_score < dec_score) {
if (print) {
printf("/ %d ", best_quot);
solve_helper(best_quot, true);
}
return best_quot_score;
}
if (print && n > 0) {
printf("- %d ", n - 1);
solve_helper(n - 1, true);
}
return dec_score;
}
int main() {
int n = 75;
printf("%d ", n);
solve(n, true);
printf("n");
}
Output
75 / 25 / 5 - 4 / 2 - 1 - 0
Iterative
TBD
I see this a recursive or iterative problem.
OP's approach hints at recursive.
A recursive solution follows:
At each step, code counts the steps of the various alternatives:
steps(n) = min(
steps(factor1_of_n) + 1,
steps(factor2_of_n) + 1,
steps(factor3_of_n) + 1,
...
steps(n-1) + 1)
The coded solution below is inefficient, but it does explore all possibilities and gets to the answer.
int solve_helper(int n, bool print) {
int best_quot = 0;
int best_quot_score = INT_MAX;
int quot;
for (int p = 2; p <= (quot = n / p); p++) {
int rem = n % p;
if (rem == 0 && quot > 1) {
int score = solve_helper(quot, false) + 1;
if (score < best_quot_score) {
best_quot_score = score;
best_quot = quot;
}
}
}
int dec_score = n > 0 ? solve_helper(n - 1, false) + 1 : 0;
if (best_quot_score < dec_score) {
if (print) {
printf("/ %d ", best_quot);
solve_helper(best_quot, true);
}
return best_quot_score;
}
if (print && n > 0) {
printf("- %d ", n - 1);
solve_helper(n - 1, true);
}
return dec_score;
}
int main() {
int n = 75;
printf("%d ", n);
solve(n, true);
printf("n");
}
Output
75 / 25 / 5 - 4 / 2 - 1 - 0
Iterative
TBD
edited Nov 25 '18 at 23:47
answered Nov 25 '18 at 23:40
chuxchux
84.2k873154
84.2k873154
Thank you for your answer, I'll test it when I reach home. However, I want to try and find how to do it more efficient, in a recursive way.
– אברגיל יעקובו
Nov 26 '18 at 7:03
Remember to make it function right, before efficiency. It is easy to make faster, yet not arrive at the optimal solution.
– chux
Nov 26 '18 at 7:18
add a comment |
Thank you for your answer, I'll test it when I reach home. However, I want to try and find how to do it more efficient, in a recursive way.
– אברגיל יעקובו
Nov 26 '18 at 7:03
Remember to make it function right, before efficiency. It is easy to make faster, yet not arrive at the optimal solution.
– chux
Nov 26 '18 at 7:18
Thank you for your answer, I'll test it when I reach home. However, I want to try and find how to do it more efficient, in a recursive way.
– אברגיל יעקובו
Nov 26 '18 at 7:03
Thank you for your answer, I'll test it when I reach home. However, I want to try and find how to do it more efficient, in a recursive way.
– אברגיל יעקובו
Nov 26 '18 at 7:03
Remember to make it function right, before efficiency. It is easy to make faster, yet not arrive at the optimal solution.
– chux
Nov 26 '18 at 7:18
Remember to make it function right, before efficiency. It is easy to make faster, yet not arrive at the optimal solution.
– chux
Nov 26 '18 at 7:18
add a comment |
For n > 1 you have to test for all divisors between sqrt(n) and n - 1 (inclusive), not simply the largest.
Here is a non-recursive solution:
#include <stdio.h>
int main ()
{
// assume N non-negative
int N = 150;
int next[N + 1];
int steps[N + 1];
steps[0] = 0;
for (int n = 1; n <= N; n++) {
next[n] = n - 1;
steps[n] = 1 + steps[n-1];
for (int i = n - 1; i * i >= n; i--) {
if (n % i == 0) {
int tmp = 1 + steps[i];
if (tmp < steps[n]) {
steps[n] = tmp;
next[n] = i;
}
}
}
}
printf ("%d: %d stepsn", N, steps[N]);
printf ("%d", N);
int tmp = N;
while (tmp != 0) {
tmp = next[tmp];
printf (" -> %d", tmp);
}
printf ("n");
}
add a comment |
For n > 1 you have to test for all divisors between sqrt(n) and n - 1 (inclusive), not simply the largest.
Here is a non-recursive solution:
#include <stdio.h>
int main ()
{
// assume N non-negative
int N = 150;
int next[N + 1];
int steps[N + 1];
steps[0] = 0;
for (int n = 1; n <= N; n++) {
next[n] = n - 1;
steps[n] = 1 + steps[n-1];
for (int i = n - 1; i * i >= n; i--) {
if (n % i == 0) {
int tmp = 1 + steps[i];
if (tmp < steps[n]) {
steps[n] = tmp;
next[n] = i;
}
}
}
}
printf ("%d: %d stepsn", N, steps[N]);
printf ("%d", N);
int tmp = N;
while (tmp != 0) {
tmp = next[tmp];
printf (" -> %d", tmp);
}
printf ("n");
}
add a comment |
For n > 1 you have to test for all divisors between sqrt(n) and n - 1 (inclusive), not simply the largest.
Here is a non-recursive solution:
#include <stdio.h>
int main ()
{
// assume N non-negative
int N = 150;
int next[N + 1];
int steps[N + 1];
steps[0] = 0;
for (int n = 1; n <= N; n++) {
next[n] = n - 1;
steps[n] = 1 + steps[n-1];
for (int i = n - 1; i * i >= n; i--) {
if (n % i == 0) {
int tmp = 1 + steps[i];
if (tmp < steps[n]) {
steps[n] = tmp;
next[n] = i;
}
}
}
}
printf ("%d: %d stepsn", N, steps[N]);
printf ("%d", N);
int tmp = N;
while (tmp != 0) {
tmp = next[tmp];
printf (" -> %d", tmp);
}
printf ("n");
}
For n > 1 you have to test for all divisors between sqrt(n) and n - 1 (inclusive), not simply the largest.
Here is a non-recursive solution:
#include <stdio.h>
int main ()
{
// assume N non-negative
int N = 150;
int next[N + 1];
int steps[N + 1];
steps[0] = 0;
for (int n = 1; n <= N; n++) {
next[n] = n - 1;
steps[n] = 1 + steps[n-1];
for (int i = n - 1; i * i >= n; i--) {
if (n % i == 0) {
int tmp = 1 + steps[i];
if (tmp < steps[n]) {
steps[n] = tmp;
next[n] = i;
}
}
}
}
printf ("%d: %d stepsn", N, steps[N]);
printf ("%d", N);
int tmp = N;
while (tmp != 0) {
tmp = next[tmp];
printf (" -> %d", tmp);
}
printf ("n");
}
answered Nov 26 '18 at 10:22
Antoine MathysAntoine Mathys
5,8121532
5,8121532
add a comment |
add a comment |
If you start looking for a divisor with 2, and work your way up, then the last pair of divisors you find will include the largest divisor. Alternatively you can start searching with divisor = N/2 and work down, when the first divisor found will have be largest divisor of N.
True that working ones way or or down will find the largest divisor, yet it is unclear that when considering step 1, finding the largest divisor in each step will result in the fewest steps. Perhaps using a not-the-largest- divisors at an early stage results in the fewest steps?
– chux
Nov 25 '18 at 23:45
add a comment |
If you start looking for a divisor with 2, and work your way up, then the last pair of divisors you find will include the largest divisor. Alternatively you can start searching with divisor = N/2 and work down, when the first divisor found will have be largest divisor of N.
True that working ones way or or down will find the largest divisor, yet it is unclear that when considering step 1, finding the largest divisor in each step will result in the fewest steps. Perhaps using a not-the-largest- divisors at an early stage results in the fewest steps?
– chux
Nov 25 '18 at 23:45
add a comment |
If you start looking for a divisor with 2, and work your way up, then the last pair of divisors you find will include the largest divisor. Alternatively you can start searching with divisor = N/2 and work down, when the first divisor found will have be largest divisor of N.
If you start looking for a divisor with 2, and work your way up, then the last pair of divisors you find will include the largest divisor. Alternatively you can start searching with divisor = N/2 and work down, when the first divisor found will have be largest divisor of N.
answered Nov 25 '18 at 21:30
LeonardLeonard
5,34373766
5,34373766
True that working ones way or or down will find the largest divisor, yet it is unclear that when considering step 1, finding the largest divisor in each step will result in the fewest steps. Perhaps using a not-the-largest- divisors at an early stage results in the fewest steps?
– chux
Nov 25 '18 at 23:45
add a comment |
True that working ones way or or down will find the largest divisor, yet it is unclear that when considering step 1, finding the largest divisor in each step will result in the fewest steps. Perhaps using a not-the-largest- divisors at an early stage results in the fewest steps?
– chux
Nov 25 '18 at 23:45
True that working ones way or or down will find the largest divisor, yet it is unclear that when considering step 1, finding the largest divisor in each step will result in the fewest steps. Perhaps using a not-the-largest- divisors at an early stage results in the fewest steps?
– chux
Nov 25 '18 at 23:45
True that working ones way or or down will find the largest divisor, yet it is unclear that when considering step 1, finding the largest divisor in each step will result in the fewest steps. Perhaps using a not-the-largest- divisors at an early stage results in the fewest steps?
– chux
Nov 25 '18 at 23:45
add a comment |
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.
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%2f53471588%2ffind-the-minimum-number-of-steps-to-decrease-n-to-zero%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
I don't understand: for 150 I would say: 150=15*10 => 15. 15=5*3=> 5, ..., which is one step less than your proposed solution.
– Dominique
Nov 25 '18 at 20:36
1
I'm not grasping why your output has 75 as the first step for 150. Why not 15, since 150 = 15 * 10 and 15 = max(15, 10)? It looks to me like you'll get the most rapid convergence to zero by picking a and b to be the integer cofactors that are closest to sqrt(N).
– pjs
Nov 25 '18 at 20:36
1
What is your question? A different approach than the simple brute force you have shown? The code for
SomefindDivisorFunction()? Please be more specific. If it is the code for the function, then consider focusing your question on that part,– Yunnosch
Nov 25 '18 at 20:36
Did you forget a piece of the question? for example, something about whether a and b are relatively prime? otherwise, as suggested here, you should factor it so that the two factors are as close as possible to sqrt(N).
– CoffeeTableEspresso
Nov 25 '18 at 21:00
1
As the problem is stated in the question, for any positive N, we have N → −1 (since N = −1•−N and −1 = max(−1, −N)) → −2 (by decrement) → 2 (since −2 = −1•2) → 1 (by decrement) → 0 (by decrement).
– Eric Postpischil
Nov 26 '18 at 2:08