> SCHEME/RACKET: How To Code A Factorial WITHOUT using the corresponding primitive?

SCHEME/RACKET: How To Code A Factorial WITHOUT using the corresponding primitive?

Posted at: 2014-12-18 
First of all a factorial is not a primitive type (see source) ,but, that is irrelevant. Also, there is multiple ways of doing this problem. However, the best way to do this problem without recursion is some sort of loop. Be it a for loop, while loop, or a do while loop. If you learned about conditionals then I am 90% sure the next thing your going to learn are these loops. Aside from that you could solve this problem using recursion. Also, I am not posting code since this seems like a homework question. However, I pretty much gave you the answer to your problems... loops.

On a side note I forgot but if you know n ahead of time you can just simple do

int n = 3;

int factorial = n * (n-1) * (n-2);

However, that only works if you know n ahead of time and you can change the factorial variable according to n.

WAIT I CAN'T USE LOOPS AND I'M CONFUSED COULD SOMEONE CODE THIS FOR ME PLEASEEE I WILL LOVE YOU FOREVER

;;myfact.rkt

(define myfact

(lambda (n)

(cond ((zero? n)

1)

(else (* n (myfact (- n 1)))))))

>[This is just a start, because I don't have racket loaded, and had to go from memory]

>John (gnujohn)

Hi, I'm a computer science student in High School taking Intro to CS1. I need help coding a factorial such that n! = n* (n-1) * (n-2) * ....1, except I cannot use the primitive (factorial a) and I must use a conditional.

I am a beginner, my language is Beginner Student and I know basic primitives, Boolean operators, conditionals/if statements, but that's it.

I cannot come up with a way to solve this problem! Please help me!! My attempt is below, but I didn't get too far. :(