Int factorial(int n) {

int factorial(int n) {
if (n

Other urls found in this thread:

weeb.ddns.net/0/programming/c_without_standard_library_linux.txt
twitter.com/SFWRedditImages

Compiler will make them into the same machine code, so it doesn't matter

/** @brief get fucked */
int factorial(int n)
{
if (n

If someone asks for factorial(-1), you'll return 1. You need an if for negative scenarios.

>i am a literal codenigger that uses inconsistent style

unsigned int user

int factorial(int n) {
return (n

for(int o=1;n>1;n--;){
o = o*n;
}


prove me wrong

return is implied but
>n--;

I'm on a lot of caffeine and my fingers want to press every key on the keyboard to stay warm so give me a break

Factorial is non-negative only in the first place, you child. Why don't you go earn a CS degree?

Maybe but not necessarily. Depends on the language. Proper solution would be to deal with it before the error arises.

It is non-negative only, I know that. Why else do you think I'm saying this? I'm pointing out the fact that for anything negative it shouldn't give an answer, yet it clearly gives 1.

negative doesn't exist, you mong

The fuck do you mean

"Proper" solution is to just use a lookup table since the return value is an int, and a 64-bit int can't even represent a value for any input past, like, 20.

Let's just assume it would address however large a space it needed. You *still* need to solve the issue of a negative input.

Bracket-less statements are literally the devil, so neither. would be best if you want to shorthand, had not that poster been a literal retard.

>B-b-but muh fancy if statements
The amount of bugs caused by this style of coding isn't fucking worth it. If the Linux kernel suffers from bugs due to this style, I'm not convinced some faggotposter on Sup Forums will be any better.

unsigned int

lol it's 12! if you're looking at 32bits

#include

int factorial(int n)
{
return (int) gamma((double) (n + 1));
}

If someone threw a -1 in there expecting the factorial of -1 you need to tell them that there's an error and that the command makes no sense. You can't just assume their input was +1 and give them 1. That's giving an incorrect answer.

how do you pass a -1 for an unsigned int my dude

fact :: Integer -> Integer -> Integer
fact 1 = 1
fact x = x * fact(x-1)

fact :: Integer -> Integer
fact 1 = 1
fact n = n*( fact (n-1) )

That function expects 2 arguments mate

let factorial = n => n

factorial = function(n){ifelse(n

Is that fucking R?

holy shit
gamma *is* in math.h
seems like overkill for me but why not

yup

...

>ifelse function
How awful

virgins have had a hundred years to add nerd math to the standard library instead of getting laid

everything is in there

fermat's last theorem is proved in that shit on some platforms

wow.

but what about the bloat tho?

I'm really rusty with my C/C++ - Does it even matter if a lib you use contains all kinds of useless shit?

>recursion for a problem best solved via iteration

Jesus it's like you never read Code Complete.

// factorial returns 0 if input is invalid
inf factorial(int n)
{
if(n

take the red pill my friend:

weeb dot ddns dot net slash 0/programming/c_without_standard_library_linux.txt

>weeb dot ddns dot net slash 0/programming/c_without_standard_library_linux.txt
What the fuck are you doing?
weeb.ddns.net/0/programming/c_without_standard_library_linux.txt

I assume by lib you mean a dynamic library like .dll in windows or .so in linux.
dynamic libraries get loaded at program start fully into ram and your program gets the address of the function you call.

if you don't mind ram bloat, it doesn't matter.

defeating rankbrain

>dynamic libraries get loaded at program start fully into ram and your program gets the address of the function you call

And does static linking mean that since you have the source of the lib you're addressing, only the relevant parts of the code get added to the executable?

dumb question I know

depends on compiler options, but that's the idea

Interesting.
Will look into.

Sweet, thanks.

Have a nice gif.

Oh wow.
I've only started skimming this document and I can see already that's going to be a VERY informative read.
Thank you.

First one, but two changes.

int factorial(unsigned int n)
{
if (n

That's only one change

You're right, I misread it. I thought it said
if (n

Or make it tail recursive
factorial :: Integer -> Integer
factorial = h 1
where
h :: Integer -> Integer -> Integer
h acc 0 = acc
h acc n = h (n*acc) (n-1)

>Not using the clean and elegent python method
import functools

def memoize(func):
cache = func.cache = {}

@functools.wraps(func)
def wrapper(n):
if n not in cache:
cache[n] = func(n)
return cache[n]
return wrapper

def memodict(f):
class memodict(dict):
def __missing__(self, key):
ret = self[key] = f(key)
return ret
return memodict().__getitem__

@memodict
# @memoize
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)