본문 바로가기

프로그래밍/알고리즘

Programming Challenges [문제1] 3n+1문제(The 3n+1Problem)

문제 

onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem&problem=36

 

Online Judge

Our Patreons Diamond Sponsors Steven & Felix Halim Reinardus Pradhitya  Gold Sponsors --- YOUR NAME HERE ----  Silver Sponsors --- YOUR NAME HERE ----   Bronze Sponsors Christianto Handojo Contribute Bitcoin:  1ojudgeapLUjJcnUm ze67a4w3TJ6WnPxo 

onlinejudge.org

let a = 900;
let b = 1000;

let max = 0;

for(let i = a; i<= b; i++){
    let n = i;
    let arr = [];
    arr.push(n);
    while(true){
        if(n % 2 === 0){
            n = n / 2;
        }else{
            n = n * 3 + 1;
        }
        arr.push(n);
    
        if(n === 1){
            break;
        }
    }
    if(max < arr.length){
        max = arr.length;
    }

}

console.log(max);