简体   繁体   中英

How to check for division by 7 for big number in C++?

I have to check, if given number is divisible by 7, which is usualy done just by doing something like n % 7 == 0 , but the problem is, that given number can have up to 100000000, which doesn't fit even in long long .

Another constrain is, that I have only few kilobytes of memory available, so I can't use an array.

I'm expecting the number to be on stdin and output to be 1 / 0 .

This is an example

34123461273648125348912534981264376128345812354821354127346821354982135418235489162345891724592183459321864592158
0

It should be possible to do using only about 7 integer variables and cin.get() . It should be also done using only standard libraries.

you can use a known rule about division by 7 that says: group each 3 digits together starting from the right and start subtracting and adding them alternativly, the divisibility of the result by 7 is the same as the original number:

ex.:

testing 341234612736481253489125349812643761283458123548213541273468213
        549821354182354891623458917245921834593218645921580

   (580-921+645-218+593-834+921-245+917-458+623-891+354-182
    +354-821+549-213+468-273+541-213+548-123+458-283+761-643
    +812-349+125-489+253-481+736-612+234-341 
    = 1882 )
    % 7 != 0 --> NOK!

there are other alternatives to this rule, all easy to implement.

Think about how you do division on paper. You look at the first digit or two, and write down the nearest multiple of seven, carry down the remainder, and so on. You can do that on any abritrary length number because you don't have to load the whole number into memory.

七个规则的大部分可分解性在数字级别上起作用,因此在字符串上应用它们应该没有问题。

You can compute the value of the number modulo 7.

That is, for each digit d and value n so far compute n = (10 * n + d) % 7.

This has the advantage of working independently of the divisor 7 or the base 10.

I'd start by subtracting some big number which is divisible by 7.

Examples of numbers which are divisible by 7 include 700, 7000, 70000, 140000000, 42000000000, etc.

In the particular example you gave, try subtracting 280000000000(some number of zeros)0000.

Even easier to implement, repeatedly subtract the largest possible number like 70000000000(some number of zeros)0000.

You can compute the value of the number modulo 7.

That is, for each digit d and value n so far compute n = (10 * n + d) % 7.

This has the advantage of working independently of the divisor 7 or the base 10.

I solved this problem exactly the same way on one of programming contests. Here is the fragment of code you need:

int sum = 0;
while (true) {
  char ch;
  cin>>ch;
  if (ch<'0' || ch>'9') break; // Reached the end of stdin
  sum = sum*10; // The previous sum we had must be multiplied
  sum += (int) ch;
  sum -= (int) '0'; // Remove the code to get the value of the digit
  sum %= 7; 
}

if (sum==0) cout<<"1";
else cout<<"0";

This code is working thanks to simple rules of modular arithmetics. It also works not just for 7, but for any divisor actually.

N = abc

There is a simple algorithm to verify if a three-digit number is a multiple of 7:

Substitute a by x and add it to bc, being x the tens of a two-digit number multiple of 7 whose hundreds is a.

N = 154; x = 2; 2 + 54 = 56; 7|56 and 7|154

N = 931; x = 4; 4 + 31 = 35; 7|35 and 7|931

N = 665; x = 5; 5 + 65 = 70; 7|70 and 7|665

N = 341; x = 6; 6 + 41 = 47; 7ł47 and 7ł341

If N is formed by various periods the inverse additive of the result of one period must be added to the sum of the next period, this way:

N = 341.234

6 + 41 = 47; - 41 mod 7 ≡ 1; 1 + 4 + 34 = 39; 7ł39 and 7łN

N = 341.234.612.736.481

The result for 341.234 is 39. Continuing from this result we have:

-39 mod 7 ≡ 3; 3 + 5 + 6 + 1 + 2 + 1 = 18; - 18 mod 7 ≡ 3; 3 + 0 + 36 = 39; - 39 mod 7 ≡ 3; 3 + 1 + 81 = 85; 7ł85 and 7łN

This rule may be applied entirely through mental calculation and is very quick. It was derived from another rule that I created in 2.005. It works for numbers of any magnitude and for divisibility by 13.

Because I recently did work dealing with breaking up numbers, I will hint that to get specific numbers - which is what you will need with some of the other answers - think about integer division and using the modulus to get digits out of it.

If you had a smaller number, say 123 , how would you get the 1 , the 2 , and the 3 out of it? Especially since you're working in base 10...

Here's a simple way to check for divisibility by 7:

Multiply each digit beginning on the right hand side of the given number by the corresponding digit in this pattern [1,3,2,6,4,5] (or [1,3,2,-1,-3,-2]). Repeat the pattern as necessary. If the sum of the products is divisible by 7, then so is the original number.

Example: 2016 (6*1+1*3+0*2+2*6=21) is divisible by 7 since 21 is divisible by 7.

See Divisibility rules .

At first Take That Big Number in string And then sum every digit of string. at last check if(sum%7==0)

Code:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,j,sum,k;
    sum=0;
    string s;
    cin>>s;
    for(i=0;i<s.length();i++)
    {
        sum=sum+(s[i]-'0');
    }

    if(sum%7==0)
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM