1

INPUT

Integers (0 ≤ Integer < 10^1,000,000)

OUTPUT

Find input mod 3 and input mod 11

EXAMPLE

main(){

        int num;//problem here i try long long not pass because it not enough.

        scanf("%d",&num);

        printf("%d ",num%3);

        printf("%d",num%11);

}
Dasrath
  • 366
  • 2
  • 11
NutOrbit
  • 23
  • 5
  • Have a look at fmod in math.h (man fmod). –  Dec 04 '15 at 05:40
  • @DavidCullen: you must be kidding! you need all the digits in base 10 to compute `num` mod `3` or `11`. Floating point is even less appropriate than `long long` – chqrlie Dec 04 '15 at 06:13
  • Ummm 'Integers (0 ≤ Integer < 10^1,000,000)' are you sure your range is wide enough? :(( – Martin James Dec 04 '15 at 09:25
  • @Martin James The idea is certainly to consider unbounded size integer input. This is a basic premise to many encryption algorithms where data is treated as an arbitrary length integer. Even this example stems from grade school math: add the digits up and see if that sum divides by 3. Further [divisibility by 1 to 20](https://en.wikipedia.org/wiki/Divisibility_rule) – chux - Reinstate Monica Dec 04 '15 at 15:05
  • I was so tired when I read this question I didn't realize how large the range is. Can [gmp](https://gmplib.org/) handle million digit numbers? –  Dec 04 '15 at 17:39

2 Answers2

1

"Normal types in C can usually only store up to 64 bits, so you'll have to store big numbers in an array, for example, and write mathematical operations yourself. But you shouldn't reinvent the wheel here - you could try the GNU Multiple Precision Arithmetic Library for this purpose."

-AndiDog

Source:Store and work with Big numbers in C

Community
  • 1
  • 1
Jimmie
  • 397
  • 2
  • 5
  • 20
  • 2
    You can find a simpler iterative solution by reading one char at a time and performing modulo arithmetic at each step. – chqrlie Dec 04 '15 at 06:15
0

Take advantage that mod 3 and mod 11 can easily be chained one digit at a time.

#include<stdio.h>
#include<ctype.h>

void mod3_11(void) {
  int mod3 = 0;
  int mod11 = 0;
  int ch;
  while (isdigit(ch = fgetc(stdin))) {
    int digit = ch - '0';
    mod3 = (digit + mod3)%3;
    mod11 = (digit - mod11 + 11)%11;
  }
  printf("mod 3  = %d\n", mod3);
  printf("mod 11 = %d\n", mod11);
  fflush(stdout);
  }

int main(void) {
  mod3_11();
  return 0;
}

This works as each successive digit is processed, code is taking the previous "mod" * 10

// math
mod_n <-- (10*mod_n + digit)%n

mod3 <-- (mod3*10 + digit) % 3
mod3 <-- (mod3*1 + mod3*3*3 + digit) % 3   
mod3 <-- (mod3 + 0 + digit) % 3    (mod3*3*3 % 3 is 0)
mod3 <-- (mod3 + digit) % 3

mod11 <-- (mod11*10 + digit) % 11
mod11 <-- (mod11*11 - mod11*1 + digit) % 11
mod11 <-- (mod11*11 - mod11 + digit + 11) % 11  (add 11 to avoid negative numbers)
mod11 <-- (-mod11 + digit + 11) % 11

Why add 11? What's the difference between “mod” and “remainder”?

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256