Programa para encontrar el resto cuando un número grande se divide por r

Dado un Número N, la tarea es encontrar el Resto cuando N se divide por R (un Número de dos dígitos). La entrada del Número puede ser muy grande.
Ejemplos: 

Input: N = 13589234356546756, R = 13
Output: 11

Input: N = 3435346456547566345436457867978, R = 17
Output: 13
  • Obtenga el dígito de N uno por uno de izquierda a derecha.
  • Para cada dígito, combine con el siguiente dígito si es menor que R.
  • Si la combinación en algún punto supera R, tome y guarde el Remanente.
  • Repita los pasos anteriores para todos los dígitos de izquierda a derecha.

A continuación se muestra el programa que implementa el enfoque anterior: 

C++

// CPP implementation to find Remainder
// when a large Number is divided by R
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to Return Remainder
int Remainder(string str, int R)
{
    // len is variable to store the
    // length of Number string.
    int len = str.length();
 
    int Num, Rem = 0;
 
    // loop that find Remainder
    for (int i = 0; i < len; i++) {
 
        Num = Rem * 10 + (str[i] - '0');
        Rem = Num % R;
    }
 
    // Return the remainder
    return Rem;
}
 
// Driver code
int main()
{
 
    // Get the large number as string
    string str = "13589234356546756";
 
    // Get the divisor R
    int R = 13;
 
    // Find and print the remainder
    cout << Remainder(str, R);
 
    return 0;
}

C

// C implementation to find Remainder
// when a large Number is divided by R
#include <stdio.h>
#include <string.h>
 
// Function to Return Remainder
int Remainder(char str[], int R)
{
    // len is variable to store the
    // length of Number string.
    int len = strlen(str);
 
    int Num, Rem = 0;
 
    // loop that find Remainder
    for (int i = 0; i < len; i++) {
 
        Num = Rem * 10 + (str[i] - '0');
        Rem = Num % R;
    }
 
    // Return the remainder
    return Rem;
}
 
// Driver code
int main()
{
 
    // Get the large number as string
    char str[] = "13589234356546756";
 
    // Get the divisor R
    int R = 13;
 
    // Find and print the remainder
    printf("%d",Remainder(str, R));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java implementation to find Remainder
// when a large Number is divided by R
 
class GFG
{
    // Function to Return Remainder
    static int Remainder(String str, int R)
    {
        // len is variable to store the
        // length of Number string.
        int len = str.length();
     
        int Num, Rem = 0;
     
        // loop that find Remainder
        for (int i = 0; i < len; i++) {
     
            Num = Rem * 10 + (str.charAt(i) - '0');
            Rem = Num % R;
        }
     
        // Return the remainder
        return Rem;
    }
     
    // Driver code
    public static void main( String [] args)
    {
     
        // Get the large number as string
        String str = "13589234356546756";
     
        // Get the divisor R
        int R = 13;
     
        // Find and print the remainder
        System.out.println(Remainder(str, R));
     
         
    }
}
 
// This code is contributed
// by ihritik

Python 3

# Python 3 implementation to
# find Remainder when a large
# Number is divided by R
 
# Function to Return Remainder
def Remainder(str, R):
 
    # len is variable to store the
    # length of Number string.
    l = len(str)
 
    Rem = 0
 
    # loop that find Remainder
    for i in range(l):
 
        Num = Rem * 10 + (ord(str[i]) -
                          ord('0'))
        Rem = Num % R
 
    # Return the remainder
    return Rem
 
# Driver code
if __name__ == "__main__":
 
    # Get the large number
    # as string
    str = "13589234356546756"
 
    # Get the divisor R
    R = 13
 
    # Find and print the remainder
    print(Remainder(str, R))
 
# This code is contributed
# by ChitraNayal

C#

// C# implementation to find
// Remainder when a large
// Number is divided by R
using System;
 
class GFG
{
     
// Function to Return Remainder
static int Remainder(String str,
                     int R)
{
    // len is variable to store the
    // length of Number string.
    int len = str.Length;
 
    int Num, Rem = 0;
 
    // loop that find Remainder
    for (int i = 0; i < len; i++)
    {
        Num = Rem * 10 + (str[i] - '0');
        Rem = Num % R;
    }
 
    // Return the remainder
    return Rem;
}
 
// Driver code
public static void Main()
{
 
    // Get the large number as string
    String str = "13589234356546756";
 
    // Get the divisor R
    int R = 13;
 
    // Find and print the remainder
    Console.WriteLine(Remainder(str, R));
}
}
 
// This code is contributed
// by Subhadeep

PHP

<?php
// PHP implementation to find Remainder
// when a large Number is divided by R
 
// Function to Return Remainder
function Remainder($str, $R)
{
    // len is variable to store the
    // length of Number string.
    $len = strlen($str);
 
    $Num = 0; $Rem = 0;
 
    // loop that find Remainder
    for ($i = 0; $i < $len; $i++)
    {
 
        $Num = $Rem * 10 + ($str[$i] - '0');
        $Rem = $Num % $R;
    }
 
    // Return the remainder
    return $Rem;
}
 
// Driver code
 
// Get the large number as string
$str = "13589234356546756";
 
// Get the divisor R
$R = 13;
 
// Find and print the remainder
echo Remainder($str, $R);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)

Javascript

<script>
 
// Javascript implementation to find Remainder
// when a large Number is divided by R
 
// Function to Return Remainder
function Remainder(str, R)
{
    // len is variable to store the
    // length of Number string.
    var len = str.length;
 
    var Num, Rem = 0;
 
    // loop that find Remainder
    for (var i = 0; i < len; i++) {
 
        Num = Rem * 10 + (str[i] - '0');
        Rem = Num % R;
    }
 
    // Return the remainder
    return Rem;
}
 
// Driver code
// Get the large number as string
var str = "13589234356546756";
// Get the divisor R
var R = 13;
// Find and print the remainder
document.write(Remainder(str, R));
 
// This code is contributed by noob2000.
</script>
Producción: 

11

 

Complejidad de tiempo: O(L) donde L es la longitud de la string

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por pawan_asipu 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 *