Find the smallest regular number that is not less than N

后端 未结 8 2084
清歌不尽
清歌不尽 2020-12-16 19:08

Regular numbers are numbers that evenly divide powers of 60. As an example, 602 = 3600 = 48 × 75, so both 48 and 75 are divisors of a power of 60.

8条回答
  •  旧巷少年郎
    2020-12-16 19:18

    I wrote a small c# program to solve this problem. It's not very optimised but it's a start. This solution is pretty fast for numbers as big as 11 digits.

    private long GetRegularNumber(long n)
    {
        long result = n - 1;
        long quotient = result;
    
        while (quotient > 1)
        {
            result++;
            quotient = result;
    
            quotient = RemoveFactor(quotient, 2);
            quotient = RemoveFactor(quotient, 3);
            quotient = RemoveFactor(quotient, 5);
        }
    
        return result;
    }
    
    private static long RemoveFactor(long dividend, long divisor)
    {
        long remainder = 0;
        long quotient = dividend;
        while (remainder == 0)
        {
            dividend = quotient;
            quotient = Math.DivRem(dividend, divisor, out remainder);
        }
        return dividend;
    }
    

提交回复
热议问题