Reputation: 75
I have a program that is suppose to find the sum of all the numbers between 1 and 75 in the fibonacci sequence that are divisible by three and add them together. I got the program working properly the only problem I am having is being able to display such a large number. I am told that the answer should be 15 digits. I have tried long long, long double, unsigned long long int and none of those produce the right output (they produce a negative number).
code:
long fibNum(int kth, int nth);
int main()
{
int kTerm;
int nTerm;
kTerm = 76;
nTerm = 3;
std::cout << fibNum(kTerm, nTerm) << std::endl;
system("pause");
return 0;
}
long fibNum(int kth, int nth)
{
int term[100];
long firstTerm;
long secondTerm;
long exactValue;
int i;
term[1] = 1;
term[2] = 1;
exactValue = 0;
do
{
firstTerm = term[nth - 1];
secondTerm = term[nth - 2];
term[nth] = (firstTerm + secondTerm);
nth++;
}
while(nth < kth);
for(i = 1; i < kth; i++)
{
if(term[i] % 3 == 0)
{
term[i] = term[i];
}
else
term[i] = 0;
exactValue = term[i] + exactValue;
}
return exactValue;
I found out that the problem has to do with the array. The array cannot store the 47th term which is 10 digits. Now I have no idea what to do
Upvotes: 1
Views: 2856
Reputation: 1130
The array cannot store the 47th term which is 10 digits.
This indicates that your architecture has a type long
with just 32 bits. That is common for 32-bit architecture. 32 bits cover 9 digit numbers and low 10-digit numbers, to be precise 2.147.483.647 for long
and 4.294.967.295 for unsigned long
.
Just change your long
types to long long
or unsigned long long
, including the return type of fibNum
. That would easily cover 18 digits.
Upvotes: 0
Reputation: 1
What you can do is take char s[15]
and int i=14,k
,
and then go for while loop till sum!=0
Under while body
k=n%10;
s[i]=k+48;
n=n/10;
i--;
Upvotes: 0
Reputation: 263197
Type long long
is guaranteed to be at least 64 bits (and is exactly 64 bits on every implementation I've seen). Its maximum value, LLONG_MAX
is at least 263-1, or 9223372036854775807
, which is 19 decimal digits -- so long long
is more than big enough to represent 15-digit numbers.
Just use type long long
consistently. In your code, you have one variable of type long double
, which has far more range than long long
but may have less precision (which could make it impossible to determine whether a given number is a multiple of 3.)
You could also use unsigned long long
, whose upper bound is at least 264-1, but either long long
or unsigned long long
should be more than wide enough for your purposes.
Displaying a long long
value in C++ is straightforward:
long long num = some_value;
std::cout << "num = " << num << "\n";
Or if you prefer printf
for some reason, use the "%lld"
format for long long
, "%llu"
for unsigned long long
.
(For integers too wide to fit in 64 bits, there are software packages that handle arbitrarily large integers; the most prominent is GNU's GMP. But you don't need it for 15-digit integers.)
Upvotes: 1