📜  检查大数是否可以被75整除

📅  最后修改于: 2021-06-26 13:38:50             🧑  作者: Mango

给定一个非常大的字符串形式的数字,任务是检查数字是否可以被75整除。

例子:

Input: N = 175
Output: No 

Input: N = 100000000000000000754586672150
Output: Yes

方法:仅当数字可被3整除(如果数字的总和可被3整除)和被25整除(如果最后两位被25整除)时,数字才可被75整除。

下面是检查给定数字是否可以除以75的实现。

C++
// C++ implementation to check the number
// is divisible by 75 or not
#include 
using namespace std;
  
// check divisibleBy3
bool divisibleBy3(string number)
{
    // to store sum of Digit
    int sumOfDigit = 0;
  
    // traversing through each digit
    for (int i = 0; i < number.length(); i++)
        // summing up Digit
        sumOfDigit += number[i] - '0';
  
    // check if sumOfDigit is divisibleBy3
    if (sumOfDigit % 3 == 0)
        return true;
  
    return false;
}
  
// check divisibleBy25
bool divisibleBy25(string number)
{
    // if a single digit number
    if (number.length() < 2)
        return false;
  
    // length of the number
    int length = number.length();
  
    // taking the last two digit
    int lastTwo = (number[length - 2] - '0') * 10
                  + (number[length - 1] - '0');
  
    // checking if the lastTwo digit is divisibleBy25
    if (lastTwo % 25 == 0)
        return true;
  
    return false;
}
  
// Function to check divisibleBy75 or not
bool divisibleBy75(string number)
{
  
    // check if divisibleBy3 and divisibleBy25
    if (divisibleBy3(number) && divisibleBy25(number))
        return true;
  
    return false;
}
  
// Drivers code
int main()
{
    string number = "754586672150";
  
    // divisible
    bool divisible = divisibleBy75(number);
  
    // if divisibleBy75
    if (divisible)
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation to check the number
// is divisible by 75 or not
  
import java.io.*;
  
class GFG {
  
  
// check divisibleBy3
static boolean divisibleBy3(String number)
{
    // to store sum of Digit
    int sumOfDigit = 0;
  
    // traversing through each digit
    for (int i = 0; i < number.length(); i++)
        // summing up Digit
        sumOfDigit += number.charAt(i) - '0';
  
    // check if sumOfDigit is divisibleBy3
    if (sumOfDigit % 3 == 0)
        return true;
  
    return false;
}
  
// check divisibleBy25
static  boolean divisibleBy25(String number)
{
    // if a single digit number
    if (number.length() < 2)
        return false;
  
    // length of the number
    int length = number.length();
  
    // taking the last two digit
    int lastTwo = (number.charAt(length - 2) - '0') * 10
                + (number.charAt(length - 1) - '0');
  
    // checking if the lastTwo digit is divisibleBy25
    if (lastTwo % 25 == 0)
        return true;
  
    return false;
}
  
// Function to check divisibleBy75 or not
static boolean divisibleBy75(String number)
{
  
    // check if divisibleBy3 and divisibleBy25
    if (divisibleBy3(number) && divisibleBy25(number))
        return true;
  
    return false;
}
  
// Drivers code
  
    public static void main (String[] args) {
    String number = "754586672150";
  
    // divisible
    boolean divisible = divisibleBy75(number);
  
    // if divisibleBy75
    if (divisible)
        System.out.println( "Yes");
    else
        System.out.println( "No");
    }
}
// This code is contributed 
// by inder_verma..


Python3
# Python 3 implementation to check the 
# number is divisible by 75 or not
  
# check divisibleBy3
def divisibleBy3(number):
      
    # to store sum of Digit
    sumOfDigit = 0
  
    # traversing through each digit
    for i in range(0, len(number), 1):
          
        # summing up Digit
        sumOfDigit += ord(number[i]) - ord('0')
  
    # check if sumOfDigit is divisibleBy3
    if (sumOfDigit % 3 == 0):
        return True
  
    return False
  
# check divisibleBy25
def divisibleBy25(number):
      
    # if a single digit number
    if (len(number) < 2):
        return False
  
    # length of the number
    length = len(number)
  
    # taking the last two digit
    lastTwo = ((ord(number[length - 2]) - 
                ord('0')) * 10 + 
               (ord(number[length - 1]) - ord('0')))
  
    # checking if the lastTwo digit 
    # is divisibleBy25
    if (lastTwo % 25 == 0):
        return True
  
    return False
  
# Function to check divisibleBy75 or not
def divisibleBy75(number):
      
    # check if divisibleBy3 and divisibleBy25
    if (divisibleBy3(number) and 
        divisibleBy25(number)):
        return True
  
    return False
  
# Driver Code
if __name__ == '__main__':
    number = "754586672150"
  
    # divisible
    divisible = divisibleBy75(number)
  
    # if divisibleBy75
    if (divisible):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by 
# Surendra_Gangwar


C#
// C# implementation to check the number
// is divisible by 75 or not
using System;
  
class GFG 
{
  
// check divisibleBy3
static bool divisibleBy3(string number)
{
    // to store sum of Digit
    int sumOfDigit = 0;
  
    // traversing through each digit
    for (int i = 0; i < number.Length; i++)
      
        // summing up Digit
        sumOfDigit += number[i] - '0';
  
    // check if sumOfDigit is divisibleBy3
    if (sumOfDigit % 3 == 0)
        return true;
  
    return false;
}
  
// check divisibleBy25
static bool divisibleBy25(string number)
{
    // if a single digit number
    if (number.Length < 2)
        return false;
  
    // length of the number
    int length = number.Length;
  
    // taking the last two digit
    int lastTwo = (number[length - 2] - '0') * 10 + 
                  (number[length - 1] - '0');
  
    // checking if the lastTwo digit
    // is divisibleBy25
    if (lastTwo % 25 == 0)
        return true;
  
    return false;
}
  
// Function to check divisibleBy75 or not
static bool divisibleBy75(string number)
{
  
    // check if divisibleBy3 and divisibleBy25
    if (divisibleBy3(number) && divisibleBy25(number))
        return true;
  
    return false;
}
  
// Driver Code
public static void Main () 
{
    string number = "754586672150";
      
    // divisible
    bool divisible = divisibleBy75(number);
      
    // if divisibleBy75
    if (divisible)
        Console.WriteLine( "Yes");
    else
        Console.WriteLine( "No");
}
}
  
// This code is contributed 
// by inder_verma..


PHP


输出:
No

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。