DEV Community

Discussion on: Daily Challenge #62 - Josephus Survivor

Collapse
 
lordapple profile image
LordApple • Edited

C++

    constexpr auto Josephus = [](int max, int step, const auto fn){
        if(step == 2){
            return ~(1 << static_cast<int>(log2(max * 2))) & ((max << 1) | 1);
        }

        if(max == 1){
            return 1;
        }
        return (fn(max - 1, step, fn) + step - 1) % max + 1;
    };