(calculating the sum of long long numbers)
whenever I input a long long number the program returns always -1 or -2 anyone knows why?
#include <iostream>
#include <math.h>
#include <string>
#include<iomanip>
using namespace std;
int main()
{
int n,i;
long long x1,x2,s;
do{
cin>>n;
}while(n<1);
long long t1[n];
long long t2[n];
string ch1,ch2;
i=0;
do{
do{
scanf("%lld %lld", &x1, &x2);
ch1=to_string(x1);
ch2=to_string(x2);
}while((ch1.length()>pow(10,5)) || (ch2.length()>pow(10,5)));
t1[i]=x1;
t2[i]=x2;
i++;
}while(i<n);
for(i=0;i<n;i++){
s=t1[i]+t2[i];
cout<<s<<endl;
}
return 0;
}
can't find a solution please help! and thx in advance!
example input:
1
14444444444444444111115556
55656464684646464646647676
output
-2
//edit:
Is there a way to calculate the sum of 100000 :) decimal digits numbers like the ones in the example using standard libraries ?
Fundamental integral types such as long long have limited range that they can represent. If a calculation causes results in a larger result than can be represented, it will overflow. Signed integer overflow has undefined behaviour in C++.
It is possible to represent an infinite range of numbers (in theory, memory limits the practice) by using an array of integers to represent different parts of the number. This technique is called arbitrary precision arithmetic.
There is no implementation of arbitrary precision types in the C++ standard library, so either you must implement one yourself, or you can use a library implemented by others.
Related
Is there any way I can preform calculations on numbers like 5000000000000000000000 in C++? I tried this:
#include<iostream>
using namespace std;
int main()
{
unsigned long long int num1, num2;
num1 = 50000000000000000;
num2 = 50000000000000000;
cout << num1*num2;
}
Any help?
is there any variable larger than unsigned long long int in C++
There are no standard integer types larger than unsigned long long. Floating point types have a larger range, but they lack precision in the range of high absolute values.
It is possible to make calculations with arbitrarily larger numbers by representing them with arrays where each elements is a "digit" of radix equal to the number of representable values of the element. Operations on such representation are called "arbitrary precision arithmetic". As for multiplication of arbitrary precision numbers, that is a well researched area. There are published algorithms such as the Schönhage–Strassen and Toom–Cook.
Such operations are not included in the standard library, but several open source implementations exist.
With user given number (n) for example n=5, I calculate the sum of 10000,10001,10002....99999.
Works up until n=17, then I get a negative number or eventually a zero.
So my question is how do I store a number bigger than unsigned long long lets me
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
unsigned long long n, sum;
cin>>n;
unsigned long long start=pow(10, n-1);
unsigned long long finish=pow(10, n)-1;
sum=((finish-start+1)*(start+finish))/2;
cout<<sum<<endl;
return 0;
}
In case you don't want bother with installing some big number library like GMP, or you do it for some site like code wars then you have basically two options:
A) create your own data type to hold bigger numbers ( in this case not really recommended, as number may be insanely huge).
B) hold it as a string/char table and write a function to process adding values.
I am solving FAST MULTIPLICATION on SPOJ.
My solution looks like this:
#include<bits/stdc++.h>
using namespace std;
int max(int a,int b)
{
if(a>b) return a;
return b;
}
long karatsuba_multiply(int x,int y)
{
if(x<10 or y<10) return x*y;
int n=max(to_string(x).length(),to_string(y).length());
int m=(int)ceil(n/2.0);
long p=(long)pow(10,m);
long a=(long)(floor(x/p));
long b=x%p;
long c=(long)(y/p);
long d=y%p;
long ac=karatsuba_multiply(a,c);
long bd=karatsuba_multiply(b,d);
long adbc=karatsuba_multiply(a+b,c+d)-ac-bd;
return (long)(pow(10*1,2*m)*ac+pow(10*1,m)*adbc+bd);
}
int main()
{
int a,b,t;
cin>>t;
while(t--)
{
cin>>a>>b;
cout<<karatsuba_multiply(a,b)<<endl;
}
return 0;
}
This code is giving correct output on coding blocks IDE as well as other IDEs. But this solution is being marked wrong on SPOJ. Can anyone tell me what I am doing wrong?
C++ originally only supports the maximum length of unsigned long long integers as about 1.8e19.
According to the problem, the answer can shoot up to 1e100000000 which is far larger.
Ways to solve this are:
Use Strings to store integers and use the operations on strings to multiply. You can check this article
Use C++ boost library. This library supports integer operations beyond 1e19 limit.
Another method would be to use some other language which supports greater than 64-bit integer like Python or use BigInteger Class in Java
From the problem description:
Input
n [the number of multiplications <= 1000]
l1 l2 [numbers to multiply (at most 10000 decimal digits each)]
Text grouped in [ ] does not appear in the input file.
A number with 10000 decimal digits is too large to fit in an int for typical sizes of int. You need to use a differnt type for the input and to carry out the multiplication. There is no built in type that can store integers that large.
thanks for checking this question.
so i wonder how can i get output 2^64, if i input is 2^64.
in unsigned long long int, it just only reach 2^64-1 == 18446744073709551615
the point is, when input number == 18446744073709551616
the output will be "2^64"
but code that i have is :
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
unsigned long long int a;
cin >> a;
if (a == pow(2,64))
{
cout << "2^64";
}
}
so the problem is, if i input : 18446744073709551616
it will no output. how can i make the output "2^64"?
unsigned long long is 64-bits or larger. That means in some machines, it is going to be just 64-bits. In this case, you will have overflow problem in your code.
Check ULLONG_MAX (#include <climits>)
Maximum value for an object of type unsigned long long int 18446744073709551615 (2^64-1) or greater*
the actual value depends on the particular system and library implementation, but shall reflect the limits of these types in the target platform.
(From http://www.cplusplus.com/reference/climits/ ).
This means your target platform supports 64 bit unsigned long long values. Thus your limit is 2^64-1.
You can try using a big integer library, like this to work around the limitation.
The largest data type in c++ differs from compiler to compiler...but generally unsigned long long int is of course considered large!!
So to solve ur problem..,better change the if condition to pow(2,64)-1...
Other than that if you really wanna implement that condition in your project, u add a "Do u mean condition..like do u mean 'thenumber+1' ?? "and proceed..;)
I wanted to write a program that computes the number of zones made by n lines.
The first example is my code, and the second is my friend's code. I think they are trying to do the same thing, but for the case n=65535 my code gives me the wrong answer. Where is the problem in my code?
my code:
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
unsigned long long ans;
ans = (n*(n + 1) / 2) + 1;
cout << ans << endl;
return 0;
}
my friend's code:
#include <iostream>
using namespace std;
int main(void){
double n,sum;
cin>>n;
sum=n*(n+1)/2+1;
cout<<(long)sum<<endl;
return 0;
}
In your code:
int n;
ans = (n*(n + 1) / 2) + 1;
All values in the calculation are ints: n is declared as int, and plain integer constants are ints as well. Therefore the result of this calculation will also be an int. The fact that you later assign this result to a long long variable doesn't change this.
Now the result of the multiplication 65535*65536 does not fit in a 32-bit signed int, so you get a nonsense answer. Fix your program by making n a 64-bit long long.
As #Dithermaster suggests, the problem here is probably one of integer overflow.
As it stands right now, your code doesn't actually make much sense. In particular, since you've defined n as an int, and all the integer literals in the expression: (n*(n + 1) / 2) + 1 are also small enough to fit in an int, the calculation will be carried out on ints, and then (after the calculation is complete) the result will be converted to long long and assigned to ans (because you've defined ans as a long long).
What you almost certainly want is to carry out the entire calculation on long long to avoid overflow. The most obvious way to do this would be to define n as a long long instead of an int.
Your friend has avoided this by defining n as a double. This works up to a point--a typical implementation of double has a 53-bit significand, so it can be used as (essentially) a 53-bit integer type. That's obviously quite a bit more than the 16 bits that's mandated for an int, but equally obviously less than the 64 bits mandated for a long long.
There's also no point in supporting n being negative, so you could consider defining n and ans as unsigned long long instead.