Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0dd72c5

Browse files
jaewon1676prove-ability
authored andcommitted
Update 콜라츠 추측.js
1 parent 093dfa3 commit 0dd72c5

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

‎level-1/콜라츠-추측.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,17 @@ function solution(num) {
3131
}
3232
// 작업을 반복한 횟수 반환
3333
return cnt;
34+
}
35+
36+
//정답 3 - jaewon1676
37+
function solution(num) {
38+
var answer = 0;
39+
// num이 1이 아니고, answer이 500이 아니면 진행.
40+
// 두 가지 조건중 하나라도 맞지 않으면 while문 종료.
41+
while(num != 1 && answer != 500){
42+
//짝수면, 2로 나눠주고 홀수면 3을 곱하고 1을 더해준다.
43+
(num % 2 == 0 ? num = num / 2 : num = num * 3 + 1)
44+
answer++;
45+
}
46+
return num == 1 ? answer : -1
3447
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /