DEV Community

Discussion on: Daily Challenge #25 - Double Cola

Collapse
 
coyote profile image
LBcoyote • Edited

Pure mathematical solution in JS. For larger n's it can be faster than the loop version by roughly a factor of logâ‚‚ n.

function who_is_next(names,n){
    let p=2**Math.floor(Math.log2(1+--n/names.length))
    return names[Math.floor((n-(p-1)*names.length)/p)]
}
Enter fullscreen mode Exit fullscreen mode