DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in

Discussion on: Daily Challenge #25 - Double Cola

Collapse
 
coyote profile image
LBcoyote • Edited on

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