how to find factorial using def? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to find factorial using def?

14th Dec 2017, 4:25 AM
rakibul
7 Answers
+ 10
def factorial(n) (1..n).inject(:*) || 1 end puts factorial(50)
14th Dec 2017, 5:16 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 8
@Mickel thnx I like Ruby so much so just made like that 😊😊
14th Dec 2017, 5:22 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 8
def fact(n) (1..n).inject(1) {|r,i| r*i } end fact(6) => 720 @Bool you can do like this too 😊😊
15th Dec 2017, 5:17 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 5
thanku Gwen sorry for being so straight
15th Dec 2017, 6:16 AM
Ankush Kumar
Ankush Kumar - avatar
+ 4
😊😊
15th Dec 2017, 5:48 AM
Ankush Kumar
Ankush Kumar - avatar
+ 3
@Gawen I've always liked that way of writing the factorial in Ruby
14th Dec 2017, 5:19 AM
Mickel
Mickel - avatar
+ 3
yeh @gwen is right, you can check it here same thing is covered here what gwen said and not just that but you can find more simple way thanku https://www.ruby-forum.com/topic/105085
15th Dec 2017, 5:12 AM
Ankush Kumar
Ankush Kumar - avatar