Factorials Hint Needed, Please


#1

I need a hint about how to “think about” how to generate factorials, please.

I’ve cheated by using an array as follows:

function funFac(num) {
	var arr = [];
	for (var i = num; i > 0; i--) {
		arr.push(num);
		num -= 1;
	}
	console.log(arr);
	var fac = arr.reduce((total, current) => total * current);
	console.log(fac);
}

funFac(5);

But I should be able to do it the “old-fashioned” way with a loop…yet I can only manage this:

function funFac(num) {
	var arr = [];
	for (var i = num; i > 0; i--) {
		arr.push(num);
		num -= 1;
	}
	console.log(arr);
	for (i = 0; i <= arr.length; i++) {
		arr.length[i] *= arr.length[i + 1];
	}
}

funFac(4);

Which of course doesn’t work because I can’t, as I say, “think about” what a factorial “means to a computer,” so to speak…I can’t translate

// pseudo-code for purposes of illustration
function doFactorial(input)
	input.decrementUntil(0)
	display(input.addTogether(input.decrementUntil(0)))

into proper Computerese for some odd reason!

Specifically, I don’t see how to “control for” the original input, since it’s always going up…I need a way to store the original value of the input (passed as an argument when the function is first called) even while it is being multiplied against all its predecessors on the number line…

I’ve also attempted a recursive solution but again I’m stymied by the need to “control things:”

function funFac(num) {
	if (num === 0) {
		return num;
    } else {
		num *= num - 1;
		funFac(num);	
    }
}

funFac(3);

So would someone be so kind as to pass me a clue? Just a pointer in the right direction, and not a solution itself (not yet [?], anyway!)…THANKS. I’m going to keep banging my head against the wall (and my fingers over the keyboard!) but the wall can only take so much! Seriously, I’ve been putzin’ around off and on for days over this and it’s causing me to question my own potential competence as a programmer…this is a classic beginners’ exercise but damned if I can wrap my head around it!!


#2

0! is 1.

if (num === 0) {
    return 1;
}

The recursion should take a smaller argument each time…

else {
    return num * funFac(num - 1);
}

#3

Well, thanks for that – I guess there’s no way to give me a hint in a case like this?

'Cause what I’m really having problems with here is thinking about the situation. You’re usually quite loquacious, Roy – are you losing faith in me at last? :wink:

See, once I see the solution, okay, it makes sense…but having seen it, I won’t be able to now go do it myself unless I make the effort to memorize it…so it’s not like, aha, “so that’s how you do it” but “wow wish I’d thought of that”…

I was like halfway there but for some reason couldn’t see through to the other side of the problem, so to speak…I had a promising setup but then got all mixed up – for example, decreasing the variable num in the wrong place while multiplying it to the wrong thing!

Anyway, if you could shed some, I dunno, “cognitive insight” here I’d appreciate it…otherwise, I’ll continue scouring various resources. In any case, thanks a bunch!


#4

I didn’t memorize that solution. I came up with it intuitively. I don’t memorize anything.

You looked to be on the right track and I nudged you, expecting you would get an ‘Ah ha’ moment from it. Sorry to wreck your learning moment.

Let’s try to get that back on track… Recursion is a very tricky concept and one that gets my head out of shape, and I sort of know about it enough to recognize it in play, most times. It was that recognition that triggered my response. See?

The code lacked a return so couldn’t be added to the stack, and the decrementation happened outside of the call argument. Two signals that, to me there was something wrong. Again, recognition. These are things that will eventually stick to your underside as they have to some degree mine. Expect more of my responses to come from this place.


#5

Yeah, no, I wasn’t suggesting that you memorized the solution – just to be clear. I only wish I didn’t have to! I mean, I haven’t needed to so far but this was the first problem I came across where I just kept missing “something” about it…

Well, no worries, like I was thinking, maybe there’s no easy way to give a hint in this case!

Well, actually, what puzzles me is that I totally get the concept – it’s all round us in life and nature, actually – but I don’t (yet?) get its implementation in code, as code…and I don’t know why! Maybe it smacks of a kind of “incest” and so subconsciously I’ve got a cognitive block on it…?? I dunno. But the concept is quite simple and straightforward! Just can’t get the “translation” into code…

WOW, I’m shocked to hear this!!! You too?? Well, 'least you’re quite far ahead of me!

You know what, I was actually playing with returns but for some reason didn’t get it in its proper, logical place and then seemed to have totally forgotten about it as I struggled with the larger issue of translating the recursive logic into code!

Yeah, see, that’s a big part of what I mean: why in the world did I think that way???

Thanks a terrifically big bunch!! Yes, I’ll be on the lookout…


#6

We who are happy are not surprised by our limitations, but delight in them. They are stop gaps where we get to survey the surroundings and decide which gap to fill next. Each is a spur onward.

Loquacious enough?


#7

Oh, brother…“To live within limits…that is what makes the poet, the artist, the human being” – Goethe.

I hate Goethe. (Well, not really.)

ARS LONGA VITA BREVIS OCCASIO PRAECEPS EXPERIMENTVM PERICVLOSVM IVDICIVM DIFFICILE

“Life is short, the craft is long [to master], opportunity fleeting, experiment treacherous, judgment difficult” – the Hippocratic Oath guy, Hippocrates!


#8

At least now I know that I am not philosophically alone, here. I make mine up, though.


#9

Well, you’re never alone anywhere – literally – if you believe as the Hindus do – literally – that everything is just a dream in the mind of God, that we’re all just run-time subroutines of the Great Cosmic Consciousness in wicked revelry…too bad I’m stuck iterating garbage-collection! :wink:


#10

I believe in noone or no thing. I believe in nothing. The uncarved block.


#11

I don’t know what to believe. I think anything is possible…I mean, if we live in a limitless universe, then anything that can happen will happen – or already has, right? I don’t really know.

Heck, I’m still having trouble with simple loops and recursion!! Really frustrating. Good thing this stuff is fascinating in its own right!


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.