Skip to content

Instantly share code, notes, and snippets.

@eyesofkids
Last active March 4, 2023 15:51
Show Gist options
  • Save eyesofkids/3aaf96bf5e932f479477 to your computer and use it in GitHub Desktop.
Save eyesofkids/3aaf96bf5e932f479477 to your computer and use it in GitHub Desktop.
factorial.swift
func factorial(a: Int) -> Int {
let n = a
if(n == 1){
return 1
}else{
return n*factorial(n-1)
}
}
factorial(5)
factorial(6)
func factorial2(a: Int) -> Int {
return a == 1 ? a : a*factorial(a-1)
}
factorial2(5)
factorial2(6)
@seungjun-green
Copy link

seungjun-green commented Nov 8, 2021

func factorial(_ n: Int) -> Double { return (1...n).map(Double.init).reduce(1.0, *) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment