Horje
Number is divisible by 29 or not

Given a large number n, find if the number is divisible by 29.
Examples : 
 

Input : 363927598
Output : No

Input : 292929002929
Output : Yes

 

 

A quick solution to check if a number is divisible by 29 or not is to add 3 times of last digit to rest number and repeat this process until number comes 2 digit. The given number is divisible by 29 if the obtained two digit number is divisible by 29.
Number is 348, 
Three times of last digit + Rest of the number = 8*3 + 34 = 58
Since 58 is divisible by 29, 348 is also divisible by 29. 
 

 

C++

<?php
// PHP program to demonstrate
// above method to check
// divisibility by 29.
 
// Returns true if n is
// divisible by 29
// else returns false.
function isDivisible($n)
{
    // add the lastdigit*3 to
    // remaining number until
    // number becomes of only
    // 2 digit
    while (intval($n / 100))
    {
        $last_digit = $n % 10;
        $n = intval($n / 10);
        $n += $last_digit * 3;
    }
 
    // return true if number is
    // divisible by 29 another
    return ($n % 29 == 0);
}
 
// Driver Code
$n = 348;
if (isDivisible($n))
    echo "Yes";
else
    echo "No" ;
 
// This code is contributed by Sam007
?>

Javascript

<script>
// Javascript program to demonstrate
// above method to check
// divisibility by 29.
 
// Returns true if n is
// divisible by 29
// else returns false.
function isDivisible(n)
{
 
    // add the lastdigit*3 to
    // remaining number until
    // number becomes of only
    // 2 digit
    while (parseInt(n / 100))
    {
        let last_digit = n % 10;
        n = parseInt(n / 10);
        n += last_digit * 3;
    }
 
    // return true if number is
    // divisible by 29 another
    return (n % 29 == 0);
}
 
// Driver Code
let n = 348;
if (isDivisible(n))
    document.write("Yes");
else
    document.write("No") ;
 
// This code is contributed by _saurabh_jaiswal
</script>

Output : 

Yes

 

Time Complexity: O(n) where n is given number.

Space Complexity: O(1) as we are not using any extra space.




Reffered: https://www.geeksforgeeks.org


Mathematical

Related
Check if a given circle lies completely inside the ring formed by two concentric circles Check if a given circle lies completely inside the ring formed by two concentric circles
Find value of y mod (2 raised to power x) Find value of y mod (2 raised to power x)
Solve the Linear Equation of Single Variable Solve the Linear Equation of Single Variable
Print Bracket Number Print Bracket Number
Find value of (1^n + 2^n + 3^n + 4^n ) mod 5 Find value of (1^n + 2^n + 3^n + 4^n ) mod 5

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
9