Multiple exercises were added to the TypeScript track in this commit.
This commit is contained in:
14
typescript/collatz-conjecture/collatz-conjecture.ts
Normal file
14
typescript/collatz-conjecture/collatz-conjecture.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
function step(count: number, interation: number): number {
|
||||
if (count === 1) return interation
|
||||
if (count % 2 === 0) { // Even
|
||||
return step(count / 2, interation + 1)
|
||||
} else {
|
||||
return step((3 * count) + 1, interation + 1)
|
||||
}
|
||||
}
|
||||
|
||||
export function steps(count: number): number {
|
||||
if (!count || count < 1 || Math.trunc(count) !== count)
|
||||
throw new Error('Only positive integers are allowed')
|
||||
return step(count, 0)
|
||||
}
|
||||
Reference in New Issue
Block a user