Skip to content

Instantly share code, notes, and snippets.

@minsOne
Created April 18, 2015 05:35
Show Gist options
  • Select an option

  • Save minsOne/0a7cd5006360ac148d0e to your computer and use it in GitHub Desktop.

Select an option

Save minsOne/0a7cd5006360ac148d0e to your computer and use it in GitHub Desktop.
짝수이면 2로 나누고, 홀수이면 3을 곱하고 1을 더하여 최종 값이 1이 될떄까지 몇번이나 도는지 측정
두 개의 값을 입력받아 길이가 더 긴 숫자를 출력함.
import Foundation
var a = 3
var b = 5
func calcValue(var value: Int) -> Int {
var count = 0
while (value != 1) {
if value % 2 == 0 {
value /= 2
} else {
value = (value * 3) + 1
}
count++
}
return count
}
let lengthA = calcValue(a)
let lengthB = calcValue(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment