Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions Challenges/0001-The_3n+1_problem/The3n+1.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,35 @@ The input will consist in a couple of integers. All of them will be less than 1,
Output the maximum cycle length found in the range defined by the input values i and j.
*/

// naive implementation, a lot of cycle-endings are calculated over and over again

func challenge_0001(i: Int, _ j: Int) -> Int {

<#Write here your solution#>

precondition(i<=j)
return Array((i...j)).map(cycleLength).maxElement()!
}

//assert(challenge_0001(1, 10) == 20)
//assert(challenge_0001(100, 200) == 125)
//assert(challenge_0001(201, 210) == 89)
//assert(challenge_0001(900, 1000) == 174)
func cycleLength(n: Int) -> Int {
return cycle(n).count
}

func cycle(n: Int) -> [Int] {
if n==1 {
return [n]
}

return [n] + cycle(nextInCycle(n))
}

func nextInCycle(n: Int) -> Int {
if n % 2 == 0 {
return n / 2
}
else {
return n * 3 + 1
}
}

assert(challenge_0001(1, 10) == 20)
assert(challenge_0001(100, 200) == 125)
assert(challenge_0001(201, 210) == 89)
assert(challenge_0001(900, 1000) == 174)