To check divisibility of any large number by 999 | function to check divisibility ; Append required 0 s at the beginning . ; add digits in group of three in gSum ; group saves 3 - digit group ; calculate result till 3 digit sum ; Driver Code
< ? php function isDivisible999 ( $ num ) { $ n = strlen ( $ num ) ; if ( $ n == 0 && $ num [ 0 ] == '0' ) return true ; if ( $ n % 3 == 1 ) $ num = "00" . $ num ; if ( $ n % 3 == 2 ) $ num = "0" . $ num ; $ gSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i += 3 ) { $ group = 0 ; $ group += ( ord ( $ num [ $ i ] ) - 48 ) * 100 ; $ group += ( ord ( $ num [ $ i + 1 ] ) - 48 ) * 10 ; $ group += ( ord ( $ num [ $ i + 2 ] ) - 48 ) ; $ gSum += $ group ; } if ( $ gSum > 1000 ) { $ num = strval ( $ gSum ) ; $ n = strlen ( $ num ) ; $ gSum = isDivisible999 ( $ num ) ; } return ( $ gSum == 999 ) ; } $ num = "1998" ; if ( isDivisible999 ( $ num ) ) echo " Divisible " ; else echo " Not ▁ divisible " ; ? >