Dividing the integers in two separate arrays [duplicate] - c++

This question already has answers here:
Array Division - What is the best way to divide two numbers stored in an array?
(7 answers)
Closed 9 years ago.
What I am trying to do is when given two arrays:
{1,5,1,7},
{4,1}
I want to get {3,7}. BTW, 1517 / 41 = 37
I can't think of simple algorithm to accomplish this. I can't simply convert the arrays into integers and use the regular division operator, because numbers in the arrays could be very huge that integers can't hold.
I've heard that using long division can help, but when I read it on Wikipedia, it only explains how to do math (http://en.wikipedia.org/wiki/Long_division). I know how to divide two numbers.
If you were to write some code, I'd prefer c++, but doesn't matter.
Any help will be appreciated.

If you use Python, this is easy.
f = lambda L: int(''.join(map(str,L)))
a = [1,5,1,7]
b = [4,1]
print map(int,str(f(a) / f(b))) # [3, 7]

Related

How to solve this easy in c or c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
print 10**1000 % 7
In C I get a syntax error because it exceeds the memory limit I guess. Can I somehow solve this easy in C or C++, such that it would give me a modulo of 10 to the power of 1000?
In addition to that not being valid syntax in C/C++, it's bad practice in python.
If you do
pow(10,1000,7)
It will use modular exponentiation so it will be much faster than doing
10 ** 1000 % 7
you can use pow for power operation. here the size of result is large value so you need to divide the problem into smaller part and solve smaller ones to get the solution in c or you'll need to handle large numbers.
you can apply modulus rules to reduce the problem as,
10^1000 % 7 =((10%7)^1000)%7
10^1000 % 7 =(((((10^10)%7)^10)%7)^10)%7
combine these rules and use to reduce the numbers generated in steps by (mod 10).use inbuilt pow() function in c to get power of number as X^y=pow(x,y)
The most general way to solve these types of big-integer problems in small (e.g. 32-bit) registers is with exponentiation by squaring, and taking modulo at each step. E.g. 10^10 is still too big to fit in a 32-bit int, though you could probably just use a long these days.
In C (and C++ too AFAIK) this is not a valid syntax. You can use pow function for exponentiation. But keep in mind that return value of pow is double and modulo operator works for int. You need some cautions.
long result = (long)pow(10, 1000);
result = result % 7;
The easiest way to do this is to reduce the terms by the modulus. The general formula is
ab mod c ≡ (a mod c)(b mod φ(c)) mod c
So in this case you get
101000 mod 7 = (10 mod 7)(1000 mod φ(7)) mod 7.
10 mod 7 = 3
φ(7) = 6
1000 mod 6 = 4
34 = 81
81 mod 7 = 4
so 101000 mod 7 = 4
in C and C++ there's no ** operator, you have to use the pow function, that however works only for floating-point types; and the % operator works only for integral types, for FP types you have to use fmod;
anyhow, if you have insert some constant 10**n you would just write 1En (say, 1E42);
.... but there's no standard type able to hold a number as big as 1E1000; even if some FP type were able to hold it, you couldn't reliably use fmod on it, due to the finite precision of the mantissa.
So, how does Python do? Under the hood, beyond the range of "normal" types it uses an arbitrary precision library. You can do the same in your C++ program, providing such library (for example you could use GMP).
But: the expression you provide can be calculated without actually computing 10**1000 and its modulus. Read Modulus power of big numbers

Handling large integers in c++ [duplicate]

This question already has answers here:
Using extremely large integer holding 3001 digits
(2 answers)
Closed 9 years ago.
How can i handle very large integers like 2^100000000 in c++?
I found no solution for this on internet that gives an exact answer.
Is there any mechanism that gives correct value in c++ for such large integers?
What you are looking for is called arbitrary precision arithmetic, you will find numerous libraries and educational resources with some googling.
You can represent given number as a string and convert it to array with integer digits. But the simplest way to google by keywords "long arithmetic c++ library" or something.
Maybe you want to use a Computer Algebra System (CAS), which would represent your expression like this:
class Pow : public Expr {
Number base;
Number exp;
};
Pow expr = new Pow(2, 100*1000*1000);
A CAS then allows you to manipulate these expressions structurally instead of the concrete values.

Algorithm for division. [duplicate]

This question already has answers here:
Algorithm for dividing very large numbers
(5 answers)
Closed 10 years ago.
May be I am already reinventing the wheel.
Normally in C, if we have a=34 and b=5 we get a/b=6. But I need the same thing for 100 digit numbers. I wrote a class with name Int. which does a+b and a-b and a*b. (a,b,c are type Int)
I overloaded the operators << , >>, which will return number divided or multiplied with 10.
What is the best algorithm for division? (assuming I store numbers as strings with base 10".
Thanks.
Perhaps you should consult https://en.wikipedia.org/wiki/Division_%28digital%29, specifically the section on large integer methods.
You're reinventing the wheel. See: http://gmplib.org/

how to sum two or three big number? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
how to sum a large number of float number?
“BigInt” in C?
handling integer having large number of digits
I want to sum two different number .
think we have two different number that length of anyone is more than 20 number , how can I sum both ? as far as I know , I can not do this with int .
like these :
26565468416574156465651652464234245645945643526 + 6264635454513215421541412154121541544455412315
There is a bunch of libraries that can do this as well as you may implement it yourself. Check this Wikipedia article.
Take a look at this C++ Big Integer lib
You must use some BigInteger implementation. Either search for a C++ library that does that or implement it yourself.
Most common implementations store the "big integer" in an array of bytes. To add two of those, do a byte-wise addition and take care of carry (both generate and properate).
EDIT: Not necessarily bytes. Any unsigned storage like int32, int64 or whatever your machine can handle.
Miracl is a great solution and pretty standard solution :
http://www.shamus.ie/index.php?page=Downloads

multiplication of string [ containing integer], output also stored in string, How? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Inputting large numbers in c++?
Arbitrary-precision arithmetic Explanation
I need to multiply two huge huge integers, like:
a=1212121212121212121212121212121212121212121212121212;
b=1212121212121212121212121212121212121212121212121212;
I think there are no data types in C and C++ to hold this huge an integer, so I thought to keep it as a string format like:-
char *number1="1212121212121212121212121212121212121212121212121212";
char *number2="1212121212121212121212121212121212121212121212121212";
during the time of multiplication I convert it into string with help of atoi() function like:
atoi(number1)*atoi(number2);
As usual the output of this multiplication will be obviously huge, so I need to change the output in string format.
I know there is an itoa() function which converts an integer to a string but it is not compatible with all compilers. Can any body tell me what I should do in this scenario?
I am using Ubuntu-10.04 and the g++ compiler.
Since C and C++ do not offer a native type that supports big numbers, it makes no sense to call atoi() to parse such numbers. atoi() returns a native int which is capped at 2,147,483,647 on 32-bit platforms.
You can use one of the numerous bignum libraries, like GMP for instance.
I think, the best variant besides using some math libraries is to split those numbers into int arrays with some fixed limit. Then just perform multiplication using basic math multiplication methods. And do not forget about overflows.
Multiplying the large numbers is very
difficult, however we can do it by
applying the logarithm of
multiplication of two numbers formula
and now we are going know how we
derived the product of two numbers’
logarithm.
Let us consider a, m and n are positive real numbers but a does not equal to 1 which means ‘a’ belongs to R+ – {1}. Logarithm of m and n to base a are x and y respectively by satisfying ax is equal to m and ay is equal to n condition.
loga (m.n) = x + y
As we already know x = loga m and y = loga n.
loga (m.n) = loga m + loga n
logarithm of multiplication of two values is equal to summation of the same values’ logarithms. The same logarithmic fundamental can now help us in multiplying the two large numbers by adding the logarithm of those values. If you don’t have a calculator, just take the logarithmic table help to perform this.
Using atoi() is also not helpful since the number itself won't fit in integer data type.
You have to simulate the method you did in elementary school.
121
*23
----
363
242*
----
2783
The implementation is left as an exercise. You would also need to know how to add big numbers.