So, here is the problem:
Given a number n I need to calculate the amount of calls it would need to calculate the fibonacci of that number recursively and output only the last digit in a given base as a decimal. The input comes as 2 numbers being the first the number n and the second the base that the output should be in. For the output should be the case number, the first input, the second input and the result of the calculation. The program should exit when the first entry is equal to the second entry that equals 0. For example:
Input:
0 100
1 100
2 100
3 100
10 10
3467 9350
0 0
Output:
Case 1: 0 100 1
Case 2: 1 100 1
Case 3: 2 100 3
Case 4: 3 100 5
Case 5: 10 10 7
Case 6: 3467 9350 7631
I have arrived on the following formula when trying to solve this. Being c(n) the last digit of the number of calls it would take in a base b, we have that:
c(n) = (c(n-1) + c(n-2) + 1) mod b
The problem is that n can be any value from 0 to 2^63 - 1 so I really need the code to be efficient. I have tried doing in a iterative way or using dynamic programming but, although it give me the right output, it doesn't give me in a short enough time. Here is my code:
Iterative
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<unsigned long long int> v;
unsigned long long int x,y,co=0;
cin >> x >> y;
while(x||y){
co++;
v.push_back(1);
v.push_back(1);
for(int i=2;i<=x;i++) v.push_back((v[i-1]+v[i-2]+1)%y);
cout << "Case " << co << ": " << x << " " << y << " " << v[x] << endl;
cin >> x >> y;
v.clear();
}
return 0;
}
Dynamic programming
#include <iostream>
#include <vector>
using namespace std;
vector<unsigned long long int> v;
unsigned long long c(int x, int y){
if(v.size()-1 < x) v.push_back((c(x-1,y) + c(x-2,y) + )%y);
return v[x];
}
int main(){
int x,y,co=0;
cin >> x >> y;
while(x||y){
co++;
v.push_back(1);
v.push_back(1);
cout << "Case " << co << ": " << x << " " << y << " " << c(x,y) << endl;
cin >> x >> y;
v.clear();
}
return 0;
}
x and y are respectively n and b, v holds the values for c(n)
Every c in the sequence is less than b. So there are b possibilities for the value of a c. So a pair of consecutive elements [ck, ck+1] can have b2 possible values. So if you start at the beginning and calculate c1, c2, c3... you will have to calculate at most b2 of them before the sequence begins to repeat; you will come to a [ck, ck+1] that is equal to an earlier [cj, cj+1].
Then you know the length of the cycle, call it S, and you know that cn = c((n-j)mod S)+j for all n > j. That should cut your work down quite a lot.
Related
what is this function used for ? and it's not for the power function. :
#include<iostream>
using namespace std;
int main(){
int x,y;
cout<<(x^y)<<endl;/* this is the unkown (X^Y)*/
return 0;
}
The ^ operator is the bitwise XOR. Take for example 6 and 12
6 in binary is: 110
12 in binary is: 1100
The xor follows this rule: "The first or the second but not both". What does it mean? Its truth table should make it clear:
A B A^B
0 0 0
0 1 1
1 0 1
1 1 0
You can see that the only 1-bits are those where or A or B (but not both) are set.
Back to the first example:
A 1100 => 12
B 0110 => 6
A^B 1010 => 10
It's XOR. If you want more information about it see here https://en.wikipedia.org/wiki/Exclusive_or
Power function in c++ is
#include <math.h>
#include <iostream>
int main()
{
int x, y;
std::cout << "Give numbers " << std::endl;
std::cout << "x = ";
std::cin >> x;
std::cout << "y = ";
std::cin >> y;
std::cout << "Result = " << pow(x, y) << std::endl;
return 0;
}
Your version is XOR (logical operation) which is used to for example embedded systems etc.
So if I am understanding c++ and logarithms correctly. Something like this should give me the base that I am looking for?
I am having some issues, but think that this way is a correct start.
#include <iostream>
using namespace std;
int main(void)
{
int x = 2;
int y = 64;
int value = log x (y);
cout << value << endl;
return 0;
}
This should display "6", but I am not sure how to really use the logarithm library..
There are three parts of a logarithm problem. The base, the argument (also called power) and the answer. You are trying to find the number of times 2 (the base) must be multiplied to get 64 (the answer).
So instead of dealing with powers and guessing. Let's just count the number of times we divide the answer 64, by the base, until we get 1.
64 / 2 = 32
32 / 2 = 16
16 / 2 = 8
8 / 2 = 4
4 / 2 = 2
2 / 2 = 1
Here we count 6 lines, so you divided the answer 64, by the base 2, 6 times. This is the same as saying you raised 2 to the 6th power to get 64.
Hmm for that to work you would need the math.h library. If you want to do this without it. You could do a loop where you constantly divide by the base given by the user.
int base = 0;
int answer = 0;
int exponent = 0;
cout << "Please type in your base";
cin >> base;
cout << "Please type in your answer";
cin >> answer;
while (answer != 1)
{
answer /= base;
exponent++
}
cout << "The exponent is " << exponent;
The implementation of a logarithm is most times a tayler function with approximates logarithm to the base e. To get the logarithm to any other base you do something like this:
#include <cmath>
double y = std::log(x)/std::log(base);
Description:
std::log() natural logarithm to base e.
y = result
base = base of the logarithm. Like 2 or 10.
For your reference:
http://en.cppreference.com/w/cpp/numeric/math/log
http://en.cppreference.com/w/c/numeric/math/log
https://en.wikipedia.org/wiki/Logarithm
https://en.wikipedia.org/wiki/Logarithm#Change_of_base
https://en.wikipedia.org/wiki/Taylor_series
https://en.wikipedia.org/wiki/Taylor_series#Examples
Here's a better implementation without using the math.h library. I might have some superfluous code as it's been like a year since I have written in C++. (Shame on me) Thanks for your question it got me wanting to brush up on my C++!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float base = 0;
float answer = 0;
int exponent = 0;
cout.precision(4);
cout << "Please type in your base \n";
cin >> base;
cout << "Please type in your answer\n";
cin >> answer;
cout << fixed;
while (answer > base)
{
answer /= base;
exponent++;
}
cout << "The exponent is: " << exponent << endl;
cout << "The Remainder is: " << fixed << answer << endl;
return 0;
}
I want to generate random numbers inside loop but results are always same numbers.
What I'm doing wrong? Thanks.
Code
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
const char duom[] = "U1.txt";
const char rez[] = "U1_rez.txt";
void num_gen(int & x, int & y);
int main(){
srand(time(NULL));
int x, y;
ifstream fd(duom);
fd >> x >> y;
fd.close();
ofstream fr(rez);
for(int j = 1; j <= 4; j++){
num_gen(x, y);
fr << x << " + " << y << " = "<< x + y << endl;
fr << x << " - " << y << " = "<< x - y << endl;
fr << x << " * " << y << " = "<< x * y << endl;
fr << x << " / " << y << " = "<< x / y << endl;
fr << "************" << endl;
}
fr.close();
return 0;
}
void num_gen(int & x, int & y){
x = 3 + (rand() % 10);
y = 3 + (rand() % 10);
}
Result
4 + 8= 12
4 - 8= -4
4 * 8= 32
4 / 8= 0
************
4 + 9= 13
4 - 9= -5
4 * 9= 36
4 / 9= 0
************
9 + 11= 20
9 - 11= -2
9 * 11= 99
9 / 11= 0
************
12 + 8= 20
12 - 8= 4
12 * 8= 96
12 / 8= 1
************
With the advent of C++11/14 you should actually give up using srand & rand & use the more efficient RANDOM NUMBER GENERATING MACHINES declared in the header #include<random>. Illustrating in a simple example :-
#include <iostream>
#include <random> // for default_random_engine & uniform_int_distribution<int>
#include <chrono> // to provide seed to the default_random_engine
using namespace std;
default_random_engine dre (chrono::steady_clock::now().time_since_epoch().count()); // provide seed
int random (int lim)
{
uniform_int_distribution<int> uid {0,lim}; // help dre to generate nos from 0 to lim (lim included);
return uid(dre); // pass dre as an argument to uid to generate the random no
}
int main()
{
for (int i=0;i<10;++i)
cout<<random(10)<<" ";
return 0;
}
One of the outputs of the above code is :-
8 5 0 4 2 7 9 6 10 8
See, the numbers vary from 0 to 10. Give your limits in uniform_int_distribution according to your desired output. This thing seldom fails & you can generate random numbers in greater ranges without worrying about outrageous outputs like you had.
Might be because the the random method is running with time of the computer. So if in the same 1/10000 of a second you computer do all process he need to do, you might read the same number since the random method haven't refresh the value. Try to put a sleep at the end of the for (like sleep(100)) and check if the values changed.
I think your code should generate different "pseudo" random numbers between 3 and 12 at each run, subject to that more of one second has elapsed between each run. Check if all this is really what you want.
Maybe you just run it much faster than the increase in a second when you call to time(NULL), which return the number of seconds since the epoch.
Anyway, your random numbers are not very good because you use the lower order bits. I transcript this excerpt from the rand() man page:
In Numerical Recipes in C: The Art of Scientific Computing (William H.
Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New
York: Cambridge University Press, 1992 (2nd ed., p. 277)), the follow-
ing comments are made:
"If you want to generate a random integer between 1 and 10, you
should always do it by using high-order bits, as in
j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));
and never by anything resembling
j = 1 + (rand() % 10);
(which uses lower-order bits)."
I am trying to write two loops in one for loop so I looked up the syntax for multiple variables in the for loop
the problem is the second variable l isn't updating I don't know why
#include<iostream>
using namespace std;
int main ()
{
float vsum=0, lsum=0;
double nsum=0, msum=0;
float v=1, l=100000000;
for (v, l ; v<= 100000000, l >= 1 ; v++, l--)
{
vsum= vsum + 1/v;
nsum= nsum + 1/v;
lsum= lsum + 1/l;
msum= msum+ 1/l;
}
cout << " The float sum of all numbers 1 through 1/100000000 is " << vsum << endl;
cout << " The double sum of all numbers 1 through 1/100000000 is " << nsum << endl;
cout << "The float sum of all numbers 1/100000000 through 1/1 is " << lsum << endl;
cout << "The double sum of all numbers 1/100000000 through 1/1 is " << msum << endl;
cin >> vsum;
}
I guess your question is that after
float f = 100000000;
why does --f; leave f unchanged?
The answer is due to the granularity of float. The float does not have enough accuracy to store every possible integer. Clearly a 32-bit float cannot store as many integer values as a 32-bit int, for example.
The further away from 0 you get, the larger the gap gets between successive possible values of a float. On your system 100000000 - 1 is still larger than the next possible value of float below 100000000.
The rules of C++ are that when the result of the calculation is not representable exactly by a float, then it's implementation-defined whether the next-lowest value or the next-highest value is used. (So your compiler should actually document what happens here). In this case your system is using the next-highest value.
To get your intended results, make v and l be integral types, and do a float conversion in the actual calculation, e.g.
vsum += 1.f/v;
nsum += 1.0/v;
As dasblinkenlight mentions, you are only checking the second condition, but the second variable is updating just fine. Here is an abridged example that proves this.
#include<iostream>
using namespace std;
int main ()
{
float vsum=0, lsum=0;
double nsum=0, msum=0;
float v=1, l=10;
for (v, l ; v<= 10, l >= 1 ; v++, l--)
{
cout << v << " " << l << endl;
}
}
Output:
1 10
2 9
3 8
4 7
5 6
6 5
7 4
8 3
9 2
10 1
Whilst working on a personal project of mine, I came across a need to divide two very large arbitrary numbers (each number having roughly 100 digits).
So i wrote out the very basic code for division (i.e., answer = a/b, where a and b are imputed by the user)and quickly discovered that it only has a precision of 16 digits! It may be obvious at this point that Im not a coder!
So i searched the internet and found a code that, as far as i can tell, uses the traditional method of long division by making a string(but too be honest im not sure as im quite confused by it). But upon running the code it gives out some incorrect answers and wont work at all if a>b.
Im not even sure if there's a better way to solve this problem than the method in the code below!? Maybe there's a simpler code??
So basically i need help to write a code, in C++, to divide two very large numbers.
Any help or suggestions are greatly appreciated!
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std; //avoids having to use std:: with cout/cin
int main (int argc, char **argv)
{
string dividend, divisor, difference, a, b, s, tempstring = ""; // a and b used to store dividend and divisor.
int quotient, inta, intb, diff, tempint = 0;
char d;
quotient = 0;
cout << "Enter the dividend? "; //larger number (on top)
cin >> a;
cout << "Enter the divisor? "; //smaller number (on bottom)
cin >> b;
//making the strings the same length by adding 0's to the beggining of string.
while (a.length() < b.length()) a = '0'+a; // a has less digits than b add 0's
while (b.length() < a.length()) b = '0'+b; // b has less digits than a add 0's
inta = a[0]-'0'; // getting first digit in both strings
intb = b[0]-'0';
//if a<b print remainder out (a) and return 0
if (inta < intb)
{
cout << "Quotient: 0 " << endl << "Remainder: " << a << endl;
}
else
{
a = '0'+a;
b = '0'+b;
diff = intb;
//s = b;
// while ( s >= b )
do
{
for (int i = a.length()-1; i>=0; i--) // do subtraction until end of string
{
inta = a[i]-'0'; // converting ascii to int, used for munipulation
intb = b[i]-'0';
if (inta < intb) // borrow if needed
{
a[i-1]--; //borrow from next digit
a[i] += 10;
}
diff = a[i] - b[i];
char d = diff+'0';
s = d + s; //this + is appending two strings, not performing addition.
}
quotient++;
a = s;
// strcpy (a, s);
}
while (s >= b); // fails after dividing 3 x's
cout << "s string: " << s << endl;
cout << "a string: " << a << endl;
cout << "Quotient: " << quotient << endl;
//cout << "Remainder: " << s << endl;
}
system ("pause");
return 0;
cin.get(); // allows the user to enter variable without instantly ending the program
cin.get(); // allows the user to enter variable without instantly ending the program
}
There are much better methods than that. This subtractive method is arbitrarily slow for large dividends and small divisors. The canonical method is given as Algorithm D in Knuth, D.E., The Art of Computer Programming, volume 2, but I'm sure you will find it online. I'd be astonished if it wasn't in Wikipedia somewhere.