Numbers larger than long long [duplicate] - c++

This question already has answers here:
Adding numbers larger than long long in C++
(4 answers)
Closed 5 years ago.
I am writing a C++ program to generate the series of Fibonacci numbers. This is the 1, 1, 2, 3, 5... series. The 300th number in this series is 359579325206583560961765665172189099052367214309267232255589801. This is well beyond the limits of int or even unsigned long long. How can I continue to represent such large numbers?
Here's my code:
unsigned long long FibLoop(int n)
{
// Keep track of previous two numbers
unsigned long long prev[2];
prev[0] = 1;
prev[1] = 1;
// Loop
for(int i = 2; i <= n; i++)
{
prev[i % 2] = prev[0] + prev[1];
cout << i << "\t" << prev[i % 2] << endl;
}
// Return
return prev[n % 2];
}

You need to download from external libraries such as BoostMultiprecision

Related

Why does this program not give out binary output [duplicate]

This question already has answers here:
Why isn't `int pow(int base, int exponent)` in the standard C++ libraries?
(11 answers)
The most efficient way to implement an integer based power function pow(int, int)
(21 answers)
Closed 2 days ago.
I'm new to C++ and this program that I wrote does not give the output, which is a binary form of an integer input.
It does give the result in Python Tutor. But in VSCode the result is always one less than the actual binary output.
Example-
5 = 100
6 = 109
17 = 10000
#include <iostream>
#include <cmath>
int main(){
int n;
std::cout << "Enter n:- ";
std::cin >> n;
int ans = 0;
int i = 0;
while (n != 0){
int bit = n & 1;
ans = (bit * pow(10, i)) + ans;
n = n >> 1;
i++;
}
std::cout << ans;
return 0;
}
What did I do wrong?

using ``1ll<<(N-1)`` to assign a large number of value to a long long int variable [duplicate]

This question already has answers here:
What does LL mean?
(4 answers)
Closed 3 years ago.
The question I saw was stated: You are given an array A of size N. An element Ai is said to be charged if its value(Ai) is greater than or equal to Ki and Ki is the total number of subsets of the array A, that contains an element Ai.
The logic is simply the number of times a number comes as a subset is 2^N-1 times. But in some test cases, N was 4000+. so, 2^N-1 will be way beyond any variable type can hold but in editorial, the writer used 1ll<<(N-1). I know the left shift operator is X<<Y = X*2^Y. But what is this 1ll? And how is it able to store such large value?
#include <bits/stdc++.h>
#define M 1000000007
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
long long arr[N];
for (int i = 0; i < N; i++)
cin >> arr[i];
if (N >= 64)
cout << 0 << endl;
else {
long long val = (1ll << (N - 1));
long long ans = 0;
for (int i = 0; i < N; i++)
if (arr[i] >= val)
ans = (ans + arr[i] % M) % M;
cout << ans << endl;
}
}
}
1<<N-1 explains 2^N-1 but does the 1ll means that it can take upto long long and long long val = 1ll<<N-1; means it can go upto 128bit?
But what is this 1ll?
The 1ll is an integer literal. 1 is an an decimal literal and ll is an integer suffix. The suffix ll means that the type of the integer literal is long long int. The 1 means that the value of the literal is, well, 1.
And how is it able to store such large value?
I think you are asking about the line:
long long val = (1ll << (N - 1));
Because of the if (N >= 64) .. else before, we know that N < 64. So the maximum number could be:
1ll << 63 - 1 =
1ll << 62 =
0x4000000000000000
Which is just a number that fits in long long int type. The long long int type has at least 64 bits.
Without the ll suffix, the 1 would have the type int. On architectures where int type is narrower then 64 bits, ex. 16 bits, then undefined behavior would happen. Left shifting a variable by a number greater or equal to the length in bits of the left operant is undefined behavior, see ex. this question.
If you are asking about the line :
ans = (ans + arr[i] % M) % M;
It is calculating the sum modulo #define M 1000000007. It is common to do assignments/homework that calculate sum modulo this number, see ex this or this. The algorithm calculates the sum modulo 1000000007, not the whole number, that's why it is able to store it inside long long variable.

How can we achieve last e.g. 6 digits of of Nth Fibonacci number in O(logN) time? [duplicate]

This question already has answers here:
nth fibonacci number in sublinear time
(16 answers)
Closed 6 years ago.
I have seen a task on an online test with competitive programming challenges (cannot disclose unfortunately where) to produce last (least significant) 6 digits of Nth Fibonacci number.
I have managed to come up with the following solution:
#include <iostream>
#include <cassert>
#include <tuple>
int solution(int N)
{
if(N == 0) return 0;
if(N == 1) return 1;
if(N == 2) return 1;
int a = 0;
int b = 1;
int c = 1;
for (int i = 3; i <= N; ++i) {
std::tie(a, b, c) = std::make_tuple(b, (a + b) % 1000000, (b + c) % 1000000);
}
return c;
}
int main()
{
assert(solution(8) == 21);
assert(solution(36) == 930352);
std::cout << solution(10000000) << std::endl;
}
which unfortunately has O(N) time complexity and start to run quite slow for inputs like in the last line: N > 10000000.
Anyone knows how this can be achieved in O(logN)?
There is an algorithm taking O(log_n) time to compute nth Fibonacci number using Q-Matrix. You can take a look at http://kukuruku.co/hub/algorithms/the-nth-fibonacci-number-in-olog-n, the only change you will need is to make sure it produce only last 6 digits.

Given a n digit long integer, how to access its parts? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Split an Integer into its digits c++
Given a number 4567.
In C++, how can you separately access 4, 5, 6 and 7?
The ones digit is n % 10, the tens digit is (n / 10) % 10, and so on. Be careful about negative numbers, the rules are slightly different.
Just to augment answers already here... You do long division. This gives you the least significant digits first.
n = abs(n);
while( n != 0 ) {
int r = n % 10;
n = n / 10;
cout << r << endl;
}
Output:
7
6
5
4
Obviously, this method gives you the least-significant digits first. You could of course generate that into an array so that you could access each number by its power (element 0 is 10^0, 1 is 10^1, etc...)
To go the other way, convert the number to a string. This approach will be slightly less efficient than long division. I know this question stated C++, but there's nothing wrong with using the C function itoa.
char s[33];
itoa(abs(n), s, 10);
for( char *d = s; d != 0; d++ ) {
int r = *d - '0';
cout << r << endl;
}
Output:
4
5
6
7
I'm not entirely sure about C++, but I know python at least will round always down, so to get the last digit you could use mod 10, and then divide out the last digit by 10, so, in sudo code
int num_to_test = 1234;
for (int i = 0; i < num_to_test.length; i++ ) {
print num_to_test % 10;
num_to_test = num_to_test / 10;
}
Do a little math and reduce the num as you go. Like this:
int thousands = floor(num / 1000);
num = num - thousands * 1000;
int hundreds = floor(num / 100);
num = num - hundreds * 100;
and furthermore, I hope you see where that is going.

How to reverse bit of a number (k bits) using bitwise? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C reverse bits in unsigned integer
How does this code work to reverse bits in number?
Given a number n with k bits, (n < 2^k), is there a fast way to do it using bitwise? This is my slow solution:
int reverse_bit(int n, int bit_size) {
bit_size--;
int result = 0;
while (n) {
if ((n & 1) == 1)
result += 1 * (1 << bit_size);
n >>= 1;
bit_size--;
}
return result;
}