Find sum of modulo K of first N natural number | Return sum of modulo K of first N natural numbers . ; Counting the number of times 1 , 2 , . . , K - 1 , 0 sequence occurs . ; Finding the number of elements left which are incomplete of sequence Leads to Case 1 type . ; adding multiplication of number of times 1 , 2 , . . , K - 1 , 0 sequence occurs and sum of first k natural number and sequence from case 1. ; Driver program
< ? php function findSum ( $ N , $ K ) { $ ans = 0 ; $ y = $ N / $ K ; $ x = $ N % $ K ; $ ans = ( $ K * ( $ K - 1 ) / 2 ) * $ y + ( $ x * ( $ x + 1 ) ) / 2 ; return $ ans ; } $ N = 10 ; $ K = 2 ; echo findSum ( $ N , $ K ) ; ? >