Comprobar si un número grande es divisible por 25 o no

Dado un número, la tarea es verificar si el número es divisible por 25. El número de entrada puede ser grande y puede que no sea posible almacenarlo incluso si usamos long long int.

Ejemplos: 

Input  : n = 56945250
Output : Yes

Input  : n = 1234567589333100
Output : Yes

Input  : n = 3635883959606670431112222
Output : No

Dado que el número de entrada puede ser muy grande, no podemos usar n % 25 para verificar si un número es divisible por 25 o no, especialmente en lenguajes como C/C++. La idea se basa en el siguiente hecho.

A number is divisible by 25 if its digits 
last two digits will be 0  or divisible by 25 .

Ilustración: 

For example, let us consider 769575 
Number formed by last two digits is = 75
Since 75 is divisible by 25 , answer is YES.
Let us consider 5325, we can write it as
5325 = 5*1000 + 3*100 + 2*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 25 is 0 if i greater 
than or equal to two. Note than 100, 1000,
... etc lead to remainder 0 when divided by 25.

So remainder of " 5*1000 + 3*100 + 2*10 + 5" 
divided by 25 is equivalent to remainder 
of following : 
0 + 0 + 20 + 5 = 25

Since 25 is divisible by 25, answer is yes.

C++

// C++ program to find if a number is
// divisible by 25 or not
#include<bits/stdc++.h>
using namespace std;
 
// Function to find that number divisible
// by 25 or not.
bool isDivisibleBy25(string str)
{
    // If length of string is single digit then
    // it's not divisible by 25
    int n = str.length();
    if (n == 1)
        return false;
 
    return ( (str[n-1]-'0' == 0  &&
              str[n-2]-'0' == 0) ||
   ((str[n-2]-'0')*10 + (str[n-1]-'0'))%25 == 0 );
}
 
// Driver code
int main()
{
    string str = "76955";
    isDivisibleBy25(str)?  cout << "Yes" :
                           cout << "No ";
    return 0;
}

Java

// Java program to find if a number is
// divisible by 25 or not
class IsDivisible
{
    // Function to find that number divisible
    // by 25 or not.
    static boolean isDivisibleBy25(String str)
    {
        // If length of string is single digit then
        // it's not divisible by 25
        int n = str.length();
        if (n == 1)
            return false;
      
        return ( (str.charAt(n-1)-'0' == 0  &&
                  str.charAt(n-2)-'0' == 0) ||
        ((str.charAt(n-2)-'0')*10 + (str.charAt(n-1)-'0'))%25 == 0 );
    }
     
    // main function
    public static void main (String[] args)
    {
        String str = "76955";
        if(isDivisibleBy25(str))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}

Python3

# Python 3 program to find if
# a number is divisible by 25
# or not
 
# Function to find that
# number divisible by 25 or not.
def isDivisibleBy25(st) :
 
    # If length of string is
    # single digit then it's
    # not divisible by 25
    n = len(st)
    if (n == 1) :
        return False
 
    return ((int)(st[n-1]) == 0 and ((int)(st[n-2])== 0) or
           ((int)(st[n-2])*10 + (int)(st[n-1])%25 == 0))
 
# Driver code
st = "76955"
if(isDivisibleBy25(st)) :
    print("Yes")
else :
    print("No")
     
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to find if a number
// is divisible by 25 or not
using System;
 
class IsDivisible
{
    // Function to find that number
    // divisible by 25 or not.
    static bool isDivisibleBy25(String str)
    {
        // If length of string is single digit then
        // then it's not divisible by 25
        int n = str.Length;
        if (n == 1)
            return false;
     
        return ((str[n - 1] - '0' == 0 &&
                 str[n - 2] - '0' == 0) ||
                ((str[n - 2] - '0') * 10 +
                 (str[n - 1] - '0')) % 25 == 0);
    }
     
    // Driver Code
    public static void Main ()
    {
        String str = "76955";
        if(isDivisibleBy25(str))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
 
// This code is contributed by Nitin Mittal

PHP

<?php
// PHP program to find if a number
// is divisible by 25 or not
 
// Function to find that number
//  divisible by 25 or not.
function isDivisibleBy25($str)
{
     
    // If length of string
    // is single digit then
    // it's not divisible by 25
    $n = strlen($str);
    if ($n == 1)
        return false;
 
    return ( ($str[$n - 1] -'0' == 0 &&
              $str[$n - 2] -'0' == 0) ||
            (($str[$n - 2] -'0') * 10 +
             ($str[$n - 1] - '0')) % 25 == 0 );
}
 
// Driver code
$str = "76955";
$x = isDivisibleBy25($str) ? "Yes" : "No ";
echo($x);
 
// This code is contributed by Ajit.
?>

Javascript

<script>
 
// Javascript program to find if a number
// is divisible by 25 or not
   
// Function to find that number
// divisible by 25 or not
function isDivisibleBy25(str)
{
     
    // If length of string
    // is single digit then
    // it's not divisible by 25
    n = str.length;
     
    if (n == 1)
        return false;
   
    return ((str[n - 1] -'0' == 0 &&
             str[n - 2] -'0' == 0) ||
           ((str[n - 2] -'0') * 10 +
            (str[n - 1] - '0')) % 25 == 0);
}
   
// Driver Code
var str = "76955";
var x = isDivisibleBy25(str) ? "Yes" : "No";
 
document.write (x);
 
// This code is contributed by bunnyram19
 
</script>

Producción: 

No

Complejidad de tiempo: O (1), ya que no estamos usando ningún bucle para atravesar.

Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.
Este artículo es una contribución de DANISH_RAZA . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *