-
Description Using the reduce methods
Code fun fact(n: Int): Int { return (1..n).reduce { acc, i -> acc * i } }
Test Cases // You can test using JUnit or KotlinTest. JUnit is shown below // TODO: replace this example test with your own, this is just here to demonstrate usage. // TODO: replace with whatever your package is called import kotlin.test.assertEquals import org.junit.Test class FactTest { fun testFact() { assertEquals(2, fact(2)) assertEquals(720, fact(6)) assertEquals(3628800, fact(10)) } }
Output:
-
Code - fun fact(n: Int): Int {
return (1..n).fold(1) { acc, i -> acc * i }- return (1..n).reduce { acc, i -> acc * i }
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}