Check if a large number is divisible by 13 or not | Returns true if number is divisible by 13 else returns false ; Append required 0 s at the beginning . ; Same as strcat ( num , "00" ) ; in c . ; Same as strcat ( num , "0" ) ; in c . ; Alternatively add / subtract digits in group of three to result . ; Store group of three numbers in group variable . ; Generate alternate series of plus and minus ; Driver code
< ? php function checkDivisibility ( $ num ) { $ length = strlen ( $ num ) ; if ( $ length == 1 && $ num [ 0 ] == '0' ) return true ; if ( $ length % 3 == 1 ) { $ num += "00" ; $ length += 2 ; } else if ( $ length % 3 == 2 ) { $ num += "0" ; $ length += 1 ; } $ sum = 0 ; $ p = 1 ; for ( $ i = $ length - 1 ; $ i >= 0 ; $ i -- ) { $ group = 0 ; $ group += $ num [ $ i -- ] - '0' ; $ group += ( $ num [ $ i -- ] - '0' ) * 10 ; $ group += ( $ num [ $ i ] - '0' ) * 100 ; $ sum = $ sum + $ group * $ p ; $ p *= ( -1 ) ; } $ sum = abs ( $ sum ) ; return ( $ sum % 13 == 0 ) ; } $ number = "83959092724" ; if ( checkDivisibility ( $ number ) ) echo ( $ number . " ▁ is ▁ divisible ▁ by ▁ 13 . " ) ; else echo ( $ number . " ▁ is ▁ not ▁ divisible ▁ by ▁ 13 . " ) ; ? >