While working through some Coderbyte challenges, I was able to solve the following problem recursively, but was hoping to get some feedback on how I can improve it.
Have the function AdditivePersistence(num) take the num parameter being passed which will always be a positive integer and return its additive persistence which is the number of times you must add the digits in num until you reach a single digit.
For example: if num is 2718 then your program should return 2 because 2 + 7 + 1 + 8 = 18 and 1 + 8 = 9 and you stop at 9.
My submitted, working recursive solution is below. How can I place "count" into my function without letting it get "reset" every time I recurse?
var count = 0;
function AdditivePersistence(num) {
count = 0;
if (num < 10) {
return count;
}
if (num > 10) {
count++;
AdditivePersistence('' + num.split("").reduce(function(a,b) {
return parseInt(a) + parseInt(b)
}));
}
}
Here's my broken attempt at moving the counter within the function... would appreciate any pointers for my beginner-self. Beyond just fixing the code, I'd love it if there are other great methods for solving this puzzle!
function AdditivePersistence(num) {
var count = 0;
(function recurse(num) {
if (num < 10) {
return count;
}
if (num > 10) {
count++;
recurse('' + num.split("").reduce(function(a,b) {
return parseInt(a) + parseInt(b);
}));
}
})();
return count;
}
Edit: I just tried with a while loop below
function AdditivePersistence(num) {
var count = 0;
while (num >= 10) {
count++
num = num.toString().split('').reduce(function(a,b) {
return parseInt(a) + parseInt(b);
})}
return count;
}
Many thanks in advance!
While working through some Coderbyte challenges, I was able to solve the following problem recursively, but was hoping to get some feedback on how I can improve it.
Have the function AdditivePersistence(num) take the num parameter being passed which will always be a positive integer and return its additive persistence which is the number of times you must add the digits in num until you reach a single digit.
For example: if num is 2718 then your program should return 2 because 2 + 7 + 1 + 8 = 18 and 1 + 8 = 9 and you stop at 9.
My submitted, working recursive solution is below. How can I place "count" into my function without letting it get "reset" every time I recurse?
var count = 0;
function AdditivePersistence(num) {
count = 0;
if (num < 10) {
return count;
}
if (num > 10) {
count++;
AdditivePersistence('' + num.split("").reduce(function(a,b) {
return parseInt(a) + parseInt(b)
}));
}
}
Here's my broken attempt at moving the counter within the function... would appreciate any pointers for my beginner-self. Beyond just fixing the code, I'd love it if there are other great methods for solving this puzzle!
function AdditivePersistence(num) {
var count = 0;
(function recurse(num) {
if (num < 10) {
return count;
}
if (num > 10) {
count++;
recurse('' + num.split("").reduce(function(a,b) {
return parseInt(a) + parseInt(b);
}));
}
})();
return count;
}
Edit: I just tried with a while loop below
function AdditivePersistence(num) {
var count = 0;
while (num >= 10) {
count++
num = num.toString().split('').reduce(function(a,b) {
return parseInt(a) + parseInt(b);
})}
return count;
}
Many thanks in advance!
Share Improve this question edited Jun 16, 2015 at 18:57 tsaiDavid asked Jun 16, 2015 at 18:16 tsaiDavidtsaiDavid 3552 gold badges6 silver badges14 bronze badges 7- Use tail-recursion (that is, pass the 'accumulator' variable as the last param of a recursive function). – raina77ow Commented Jun 16, 2015 at 18:20
- 2 In your first code block you are hiding the outer count variable by re-declaring it btw. – alediaferia Commented Jun 16, 2015 at 18:21
-
1
Your problem is the scope of
var count = 0;
which you somehow expect to be global. – Bergi Commented Jun 16, 2015 at 18:30 -
1
Note that
num < 10
andnum > 10
doesn't cover all the possible values ofnum
. – Matt Burland Commented Jun 16, 2015 at 18:33 - Hi all - sorry about that, my actual code didn't have a count declared within the function (of the first code block) - that was my mistake in transferring onto here, apologies. – tsaiDavid Commented Jun 16, 2015 at 18:43
3 Answers
Reset to default 5The idea is simple
AdditivePersistence(n):
if n < 10
return 0
else
return 1 + AdditivePersistence(sum-of-digits(n))
Strictly speaking, there's no need for the recursion here - that's essentially a normal while
loop.
I'm going to extend @georg's answer and provide a full implementation
var additivePersistance = (function () {
function sumOfDigits (n) {
var ret = 0;
n.toString().split('').forEach(function (i) {
ret += parseInt(i, 10);
});
return ret;
}
return function additivePersistance (n) {
if (n < 10) {
return 0;
}
return additivePersistance(sumOfDigits(n)) + 1;
}
}());
This implementation hides the sumOfDigits as a helper method using a closure.
additivePersistance(2718); // 2
This closure idea can also serve to create a psudo static variable in the recursive function. Follow this form.
var func = (function () {
var staticCounter = 0;
return function func() {
if (staticCounter++ > 20) {
return 0;
}
return func() + 1;
};
}());
Here the body of the inner func method is using the variable staticCounter accross all calls to the outter func.
var count = 0;
function AdditivePersistence(num)
{
// make the number into a string so that each digit can be taken
var num_string = num.toString();
// this will hold each digit
var numbers = [];
// iterate through each digit as a character
for(var i = 0; i < num_string.length; ++i)
{
// add the character converted to a number into the numbers array
numbers.push(parseInt(num_string[i]));
}
// this will hold the value of all the digits added together
var total = 0;
// iterate through the digits
for(var i = 0; i < numbers.length; ++i)
{
// add each digit to the total
total += numbers[i];
}
// if larger than the total
if(total > 10)
{
// increase the count
++count;
// redo it again
AdditivePersistence(total);
}
else
{
// return the total amount of tries
return (++count);
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744374838a4571134.html
评论列表(0条)