write code that calculates a factorial, or lose your Jow Forums membership.
Write code that calculates a factorial, or lose your Jow Forums membership
Other urls found in this thread:
google.com
rextester.com
twitter.com
from math import factorial
fact n = product [1..n]
main = do
print fact 4
Type
[number]! in google
just for that you're losing /hm/ privileges too.
fuck you nigger
int factorial(int x) { for (int i = x; i >= 1; i--)x*=i;}
Take that retard
def fact(n):
if n == 0 or n == 1:
return 1
return n*fact(n-1)
print(fact(5))
based
>int
lmao stick to your prune juice gramps. factorials are vectors now.
Id be happy to write the code if you can explain to me what a factorial is
sure
factorial.calculate(5);
why did you include or x ==1 ???
Neck yourself zoomer. I bet you don't even know what an int is
lmao you never took a basic middle school course
int fact = 0
while(true) {
int tmp = -1;
printf("Please enter the next number in the factorial");
scanf("%d", tmp);
if (tmp == 0){
printf("%d", fact);
break;
} else if (fact == 0) {
fact = tmp;
} else {
fact = tmp*fact;
}
}
if you get the wrong answer you're a mega brainlet.
Because factorial of 1 is 1 because theres only 1 digit
int toFact = 5;
int result = 1;
for (int i = 2; i
import numpy as np
factorial = lambda N : np.multiply.reduce(np.arange(1,N+1))
int is archaic. use floats. trust me, i'm the CEO of javascript.
>you're a mega brainlet.
Using a while loop
Setting an int variable to -1 instead of using boolean values true and false
Wasting space
>lose
You can have mine. It's useless anyway
Sexy
ok then why did you have if n == 0 then if n == 1 works the same?
don't be like that bb
i dont know whats a factorial
>int is archaic. use floats. trust me, i'm the CEO of javascript.
I've been flaming javascript fags since before you were even born son
>born
you mean compiled. you're just an AI github project i left running. funfact: you were written in Scratch
Because thatposter is unironically a retard
$f = f;
$a = a;
$c = c;
$t = t;
$o = o;
$r = r;
$i = i;
$a = a;
$l = l;
echo $f, + $a, + $c, + $t, + $o, + $r, + $i, + $a, + $l;
Thinking this code is in anyway to be taken seriously
oof
That's fucking right "newfag". I was writtin from nothing. God made me from scratch himself, So next time, you might want to consult a bible before trying to beat "The Wizard" in an argument
int factorial(int n){
int arr[n] = {0};
if(n
I've been here since it was guro, so I have tenure. I don't have to do shit.
ugly but works for me. Had to look up the formula. I'm getting old
wtf
>Trying to cope with his shitty code
Watch how it's done
p = n;
while (n*=(--p)==0)return n;
Oh forgot && n>1
fn fac(n: u32) -> u32 {
if n < 2 {
1
}
else {
fac(n - 1) * n
}
}
fn main() {
for i in [0, 1, 5, 10].iter() {
println!("{}! = {}", i, fac(*i));
}
}
got this
import requests
from bs4 import BeautifulSoup
def factorial(num):
soup = BeautifulSoup(requests.get('google.com
result = soup.find_all('div', {'class':"BNeawe iBp4i AP7Wnd"})[0].find('div', {'class':"BNeawe iBp4i AP7Wnd"})
result = str(result)
return int(result[result.find('>')+1:result.rfind('
int fact(int n)
{
if (n
let rec factorial n = if n
>not realizing I am coping with how shitty this thread it by writing pajeet level code.
also this fails if n == 1 0r n == 0 but it looks cool.
Based and the only correct answer
See And thank you
jesus it's not even funny how much faster Haskell is at these calculations
ah, I see now. Thankyou this thread was actually good for something other than a quick shitpost
based s o y dev
fac[n_] := Gamma[n + 1]
(defun ! (n)
(if (< n 2) 1 (* n (! (1- n)))))
Beautiful.
Ugly and crude
Please give me an example of a factorial implementation that you find beautiful.
DATA SEGMENT
NUM DB ?
FACT DB 1H
RES DB 10 DUP ('$')
MSG1 DB "ENTER NUMBER : $"
MSG2 DB 10,13,"RESULT : $"
DATA ENDS
what the fuck
lol
Well... whatever tickles your fancy, man.
>not using a Fenwick tree
all of u should leave
Is this 8086 assembly?
Yes
Based
(define fact
(lambda (n)
(apply *
(cdr (iota (+ n 1))))))
Edge case if the user input 0
#!/usr/bin/python3
import math
while True:
print("Enter number: ", end = "")
num = int(input())
print("Factorial:", math.factorial(num))
MATHNIGGERS BTFO
perfect
It's actually O(1) complexity since it wouldn't compile or run
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * (factorial (n-1))
or something idk I normally use java
const factorial = (n) => (n
brainlet microcontroller edition, runs in O(n2)
long factorial(int n) {
long v = 1l;
int i;
for(; n > 1; n--) {
long w = v;
for(i = n; i > 1; i--) {
v += w;
}
}
return v;
}
public int fact(int n) {
if(n < 0) throw new IllegalStateException("bro what are you doing bro it can't be negative bro bro");
else if (n
def fact(i):
def z(i):
yield 1
if i > 0:
yield 1
for j in range(2,i+1):
for k in range(j):
yield sum(z(j-1)) - sum(z(j-2))
return list(z(i+1))[-1]
I worked hard on this, what do you guys think?
let fact n =
let rec loop accu = function
| 0 -> accu
| n -> loop (n * accu) (pred n) in
loop 1 n
>6993545 function calls and 15003310 continuations to compute 8!
user
anon that is not what generators were made for
def factorial(0), do: 1
def factorial(n) when n > 0, do: n * factorial(n - 1)
fact(0)
>Rust
my nigga
shit, it was for
1 REM *** user'S BASIC PROGRAM ***
10 PRINT "KILL YOURSELF, YOU INCOMPETENT FUCKING FAGGOT"
20 GOTO 20
remember to use pure functional programming practices in your lisp code. that means usage of let is banned
(defun factorial (n &optional (total 1))
(cond
((< n 1) -1)
((= n 1) total)
(t (factorial (- n 1) (* total n)))
)
)
(factorial 0) should be 1.
sorry I'm retarderu but you get the point
Is this possible with runtime of O(n^2)?
FUCK_OP!
gamma(n+1)
(defun quicksort (lis)
(if (null lis)
nil
(let* ((x (car lis)) (r (cdr lis)) (fn (lambda (a) (< a x))))
(append (quicksort (remove-if-not fn r)) (list x) (quicksort (remove-if fn r)))
)
)
)
int factorial(int n)
{
if (n
#include
int factorial(int i){
if(i
based
sub factorial(UInt $n) { [*] 1..$n }
[\code]
def factorial(int):
f= open("fun_times.txt","w+")
if int < 0:
while 1:
f.write("Minimize the window while your code runs.\n")
value = 1
if (not int == 0) AND (not int == 1):
for i in range(1,int+1):
value*= i
return value
#include
#include
Int factorial(int n) {
if (n == 1) return 1;
if (n == 2) return 2;
if (n == 3) return 6;
if (n == 4) return 16;
if (n != 4) printf("factorial to big")
return 0;
}
Here you go, Sir
int o,w,o2=1;
cin>>w;
for(o=w; o>1;o--)
{
o2=o*o2;
}
cout
Is it posible to do it with recursive only?
yes
How? Like this?
factorial (int f)
f * factorial(f-1)
you forgot the base case
O rite, thanks.
factorial (int f)
if (f==1)return 1
else return (f* factorial (f-1))
uint64_t f(uint64_t x) {
return x > 1 ? x * f(x-1) : 1;
}
: factorial dup 1 do i * loop ;