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

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 04:45:16

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 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.

Most of the divisibility by seven rules work on a digit level, so you should have no problem applying them on your string.

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.

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...

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.

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.

Rifat iqbal shova

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!