본문 바로가기

공부하자/Codility

[Codility] Lesson5. CountDiv (C#)

문제: app.codility.com/programmers/lessons/5-prefix_sums/count_div/

 

CountDiv coding task - Learn to Code - Codility

Compute number of integers divisible by k in range [a..b].

app.codility.com

 

            double a = (double)A;
            double b = (double)B;
            double result = 0;
            //for(int i = A; i <= B; i++)
            //{
            //    if (i % K == 0)
            //        result = result + 1;
            //}

            result = Math.Floor(b / K) - Math.Floor((a - 1) / K);

            return (int)result;