Figure out if a very large number is divisible by another

smvash

I am dealing with a programming question where I need to divide a very big number, of the order of 10^50000 by another of the order of 1000, in order to figure out if one is divisible by the other. So I need to use strings to store this large number.

I don't know any method to divide a string by a number. Can anyone point me to an algorithm?

Floris

Here is a start of an algorithm. It seems to do what you are asking for: it performs "long division" in 4 character chunks, and keeps track of the remainder. It prints the remainder at the end. You should be able to adapt this to your specific needs. Note - doing this in 4 bytes chunks makes it significantly faster than conventional one-character-at-a-time algorithms, but it does rely on b being small enough to fit in an int (hence the 4 character chunks).

#include <stdio.h>
#include <string.h>

int main(void) {
  char as[]="123123123123126";
  int l = strlen(as);
  int rem;
  int lm = l % 4; // the size of the initial chunk (after which we work in multiples of 4)
  char *ap=as;    // a pointer to keep track of "where we are in the string"
  int b=123;      // the divisor
  int a;
  sscanf(ap, "%4d", &a);
  while(lm++ < 4) a /= 10;
//  printf("initial a is %d\n", a);
  rem = a % b;

  for(ap = as + (l%4); ap < as + l; ap += 4) {
//  printf("remainder = %d\n", rem);
  sscanf(ap, "%4d", &a);
//  printf("a is now %d\n", a);
  rem = (a + 10000*rem) %b;
  }
  printf("remainder is %d\n", rem);
  return 0;
}

Output:

remainder is 3

This needs some cleaning up but I hope you get the idea.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Figure out if a business name is very similar to another one - Python

postgresql: Copy a column into another column when the number of rows are very large

Counting the number of objects in a list that are divisible by another number

Fastest way to check if a number is divisible by another in python

Storing a very, very large number theoretical

Mongoose - Increment very large number

Kafka very large number of topics?

DateTime in Unity as a very large number

Summing the digits of a very large number

Effiecient Algorithm for Finding if a Very Big Number is Divisible by 7

Can I run out of disk space by creating a very large number of empty files?

Number as well as the digits of a number divisible by another number(say 3)

Convert very large number stored as string into number

Copy a very large number of rows from one sheet to another, excluding blank rows in Excel 2010

How do you check whether a number is divisible by another number (Python)?

How to generate a random number that is evenly divisible by another randomly generated number

Number that are not divisible

How to plot a large number of vbar in a Bokeh figure

Matlab saves figure to a very large eps file for seemingly no reason

using modulus to find out if number is divisible by variable not working

How to assign a very large number to BigInteger?

how to handle very large number in float

How to convert a very large decimal number to a string?

Anova test in Python with a very large number of Groups

Pandas pivot table with very large number of columns

Bigquery subtraction on very large number not working as expected

Searching through a slice with a very large number of elements

How to store a very large number in c++

Find Rightmost unset bit of a very large number