So I wrote a program that utilizes the euclid algorithm to find GCD's of 2 ints.
The user enters one int (n), then the program takes every possible integer combination between 8 and n, finds their respective GCD's (recursively), and prints which GCD calculations required the most modulus operations.
I got the program working, but I get a stack overflow at around n=50, and it needs to work to at least 3000.
I've reviewed my code for a while and cannot find the problem.
#include<iostream>
#include <math.h>
using namespace std;
int cost, gcd, greatestCost, n, beginningA, beginningB, finalA, finalB, finalGCD, iteration;
void findGCD(int num1, int num2, int startingCost) {
//findGCD
//finds GCD of every combination (a,b) from i to n
//prints those with the greatest number of modulus operations
int a = num1;
int b = num2;
cost = startingCost;
cost++;
if (b%a > 0) {
//cout << "gcd(" << b << "," << a << ") = ";
findGCD(b%a, a, cost);
}
else {
gcd = a;
if (cost > greatestCost) {
greatestCost = cost;
finalA = beginningA;
finalB = beginningB;
finalGCD = gcd;
}
//cout << "gcd(" << b << "," << a << ") = " << gcd << " With a cost of: " << cost << endl;
//do next iteration (2,8), (3,8) etc...
if (++beginningA <= beginningB) { //beginning A goes from 1-i first
findGCD(beginningA, beginningB, 0);
}
else {
if (beginningA <= n) { //begin next cycle with new b value (1,9), (2,9) while b <= n
beginningA = 1; //reset to 1 so it will increment from 1-i again
cout << "At i=" << iteration++ << "; gcd(" << finalA << "," << finalB << ") = " << finalGCD <<
" took " << greatestCost << " modulus operations" << endl;
findGCD(beginningA, ++beginningB, 0);
}
else //When it tries to continue iterating with a number > n
//print the last, most intensive, iteration and stop
cout << "At i=" << iteration++ << "; gcd(" << finalA << "," << finalB << ") = " << finalGCD <<
" took " << greatestCost << " modulus operations" << endl;
}
}
}
int main() {
greatestCost = 0; //cost of the iteration with the most modulus operations
beginningA = 1;
beginningB = 8;
iteration = 8;
cout << "Enter an integer greater than 8 " << endl; //receive n from user
cin >> n;
if (n <= beginningB) //begin GCD search, granted user input > 8
cout << "Error!!! integer must be greater than 8";
else
findGCD(beginningA, beginningB, 0); //algorithm begins at (1,8)
return 0;
}
At this point the only thing I can think of as the problem is something I've done in C++ that I shouldn't (I am new to C++ and transferred over from java)
Sample Output
Things I've tried:
splitting the gcd function into 2
passing only references through the functions
First of all your explanation is unclear, from you code I understood that for every 8<=i<=n you take all possible x, y where y<=i and x<=y and calculate which gcd require most steps.
I've rewritten your code so that findGCD only finds gcd of 2 number, while incrementing some global cost variable.
#include<iostream>
#include <math.h>
using namespace std;
int cost, gcd, greatestCost, n, beginningA, beginningB, finalA, finalB, finalGCD, iteration;
int findGCD(int a, int b) {
cost++;
if (b%a > 0)
return findGCD(b%a, a);
else
return a;
}
int main() {
greatestCost = 0; //cost of the iteration with the most modulus operations
beginningA = 1;
beginningB = 8;
iteration = 8;
cout << "Enter an integer greater than 8 " << endl; //receive n from user
cin >> n;
if (n <= beginningB) //begin GCD search, granted user input > 8
cout << "Error!!! integer must be greater than 8";
else {
for ( int i = beginningB; i <= n; i++ ) {
int greatestCost = 0, gcd0 = 1, i0 = 0, j0 = 0;
for ( int t = beginningB; t <= i; t++ )
for ( int j = 1; j <= t; j++ ) {
cost = 0;
int gcd = findGCD(j, t);
if ( cost > greatestCost ) {
greatestCost = cost;
gcd0 = gcd;
i0 = t;
j0 = j;
}
}
cout << "At i=" << i << "; gcd(" << j0 << "," << i0 << ") = " << gcd0 <<
" took " << greatestCost << " modulus operations" << endl;
}
}
return 0;
}
The stack overflow you're getting is caused by using too deeply recursive calls: Every time you call a function a new stack frame (holding local variables, parameters and possibly other stuff) is created in the (call) stack. This frame is freed only when returning (normally or via exception) from the function. But with recursive calls you don't return from the first function call before returning from the second, which in turn only returns after the third and so on. Thus stack frames are piling up on the stack, which is commonly about the size of around 8 kB, until all available memory for the stack is used: That's the stack overflow (you put too much on it, thus it overflows).
This can be solved by using iteration (using loops) instead:
An outer one incrementing from 8 up to the user supplied maximum, as well as an inner one incrementing from 0 to the value of the outer loop's current iteration variable. This gives you all the pairs of values you want to operate on.
Calculating the greatest common divisor and its cost should be factored out into a function.
The only thing left is calling that function from within the loops and some how keeping track of the maximum.
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
unsigned gcd(unsigned a, unsigned b, unsigned * const cost) {
if (cost) {
*cost = 0;
}
while (b != 0) {
auto const rest = a % b;
if (cost) {
++(*cost);
}
a = b;
b = rest;
}
return a;
}
int main() {
unsigned const n = 3500;
unsigned greatestCost = 0;
vector<pair<unsigned, unsigned>> pairs;
for (unsigned b = 8; b <= n; ++b) {
for (unsigned a = 0; a <= b; ++a) {
unsigned cost;
gcd(a, b, &cost);
if (cost == greatestCost) {
pairs.emplace_back(a, b);
} else if (cost > greatestCost) {
pairs.clear();
pairs.emplace_back(a, b);
greatestCost = cost;
}
}
}
cout << "Greatest cost is " << greatestCost << " when calculating the GCD of " << endl;
for (auto const & p : pairs) {
cout << "(" << p.first << ", " << p.second << ")" << endl;
}
return 0;
}
(Live)
Note in particular that I'm not using any global variable.
The above might make you feel that recursion is an unusable, useless construct. This is not the case. Many algorithms are most cleanly expressed using recursion. When putting the recursive call as the last statement, then an optimisation known as tail call optimisation can be used: Then the called function is reusing the stack frame of the calling function, thus not using any more memory.
Unfortunately this optimisation is quite tricky to implement in a language like C++ due to various reasons.
Other languages, mostly functional ones, use it and thus also recursion instead of loops, though. An example of such a language is Scheme, which even requires implementations to be able to make that aforementioned optimisation.
As a final note: You could implement the GCD calculation using recursive calls here, since as you see the maximum depth will be 17 + 1 which should be small enough to fit on any (outside of embedded systems) call stack. I'd still go with the iterative version though: It has better performance, better fits the idiom of the language and is the "safer" way to go.
Related
I've been working on the Euler 29 problem for a few days and am having difficulty getting the mpz_t type to work correctly. The objective is to iterate through a^b for values of 2 <= a,b <= 100 and count the nonrepeat values.
Using vector I was able to store the values using pointers in an array like so:
mpz_t a;
mpz_init(a);
vector<mpz_t*> numbers;
numbers.push_back(&a);
However, when running the full program below you can see that after it inserts the first value 4, it doesn't insert any new values. This is because the temp value being compared to rop is not being set to what is already in the array, and instead is set to the value shared by rop.
#include <iostream>
#include <vector>
#include <chrono>
#include "gmp.h"
using namespace std;
int main()
{
auto start = std::chrono::high_resolution_clock::now();
int solution = 0;
bool found = false;
int r = 10;
mpz_t rop;
mpz_init(rop);
mpz_t temp;
mpz_init(temp);
vector<mpz_t*> numbers;
for(int a = 2; a <= 5; a++)
{
for(int b = 2; b <= 5; b++)
{
mpz_ui_pow_ui(rop, a, b);
for(int i = 0; i < numbers.size(); i++)
{
cout << "i: " << i << endl;
cout << "rop: ";
mpz_out_str(stdout,10,rop);
cout << endl;
mpz_set(temp,*(numbers.at(i)));
cout << " temp: ";
mpz_out_str(stdout,10,temp);
cout << endl;
r = mpz_cmp(rop,temp);
cout << " r: " << r << endl << endl;
if(r == 0)
{
found = true;
break;
}
}
if(found == false)
{
numbers.push_back(&rop);
solution++;
cout << "pushed! " << endl << endl;
}
found = false;
}
}
auto done = std::chrono::high_resolution_clock::now();
cout << "Solution: " << solution << endl << endl;
cout << "Program completed in " << std::chrono::duration_cast<std::chrono::milliseconds>(done - start).count() << " milliseconds." << endl;
}
This line of code should be setting temp equal to 4 at the start of the forloop, but instead sets it equal to rop:
mpz_set(temp,*(numbers.at(i)));
Since the problem clearly has to do with the fact I'm using pointers and passing the actual address in memory to store these mpz_t variables, how can I change the code so that it is able to work properly? I'm under the impression using the function mpz_clear(rop) after each push_back to the numbers vector wouldn't work as it releases the address from memory.
I figured out that due to the way mpz_t variables work the mpz_set function does not work with a pointer to mpz_t type variables as a parameter.
Instead, I was able to get the program to work by assigning the mpz_get_str function to a string and pushing that to a vector of strings to check for repeat values.
mpz_t rop;
mpz_init(rop);
char * num;
vector<string> numbers;
num = mpz_get_str(num,10,rop)
numbers.push_back(num);
am running these two functions that do the same calculation "summing the first N integers " then compare the run times for each one. The program works fine with small inputs, but the problem is when I input large numbers like 1000000, it calculates the first method "the iterativeSum()" then as soon as it gets to the the recursiveSum() it stops working.
am not sure but do you think that this might be because of the cout?
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void iterativeSum(int);
int RecursiveSum(int);
int main()
{
long long posInt;
std::cout << "Enter a positive integer: ";
std::cin >> posInt;
int start_s=clock();
iterativeSum(posInt);
int stop_s=clock();
int start_s1=clock();
cout << "\nThe recursive algorithm to sum the first N integers of "<< posInt << " is: "<< RecursiveSum(posInt) << endl;
int stop_s1=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)/1000 << endl;
cout << "time: " << (stop_s1-start_s1)/double(CLOCKS_PER_SEC)/1000 << endl;
return 0;
}
void iterativeSum(int posInt)
{
//positive Integer >=0
int sum = 0;
//loop through and get only postive integers and sum them up.
// postcondion met at i = 0
for(int i = 0; i <= posInt;i++)
{
sum +=i;
}
//output the positive integers to the screen
std::cout <<"\nThe iterative algorithm to sum the first N integers of " <<posInt <<" is: " << sum << "\n";
}
int RecursiveSum(int n)
{
if(n == 1) // base case
{
return 1;
}
else
{
return n + RecursiveSum(n - 1); //This is n + (n - 1) + (n - 2) ....
}
}
You may need an arbitrary precision arithmetic library like GMPlib, to avoid arithmetic overflows. And you should be afraid of stack overflow.
The call stack is often limited (to e.g. a megabyte). See this
In my program,user will be asked for 2 different options.
I chose option 2..iterative and key in any value which will then lead to the output.
However,when i choose the 1st option which is recursive,it wont output anything that its value is above 30.Meaning to say,you will see an output if key in a value of 30..& there will be no output if were to key in the value of 40 or 50.
Can anyone please test on your compiler too?Its ok if something wrong with my compiler but if there is something wrong with my code.
#include<iostream>
using namespace std;
/* Fibonacci: recursive version */
int Fibonacci_R(int n)
{
if (n <= 0) return 0;
else if (n == 1) return 1;
else return Fibonacci_R(n - 1) + Fibonacci_R(n - 2);
}
// iterative version
int Fibonacci_I(int n)
{
int fib[] = { 0, 1, 1 };
for (int i = 2; i <= n; i++)
{
fib[i % 3] = fib[(i - 1) % 3] + fib[(i - 2) % 3];
cout << "fib(" << i << ") = " << fib[i % 3] << endl;
}
return fib[n % 3];
}
int main()
{
int a, opt;
cout << "Please choose the available option:\n";
cout << "1)Recursive\n";
cout << "2)Iterative\n";
cin >> opt;
if (opt == 1)
{
cout << "Please input value:\n";
cin >> a;
Fibonacci_R(a);
cout << endl << "From recursive function" << endl;
for (int i = 1; i <= a; ++i)
cout << "fib(" << i << ") = " << Fibonacci_R(i) << endl;
cout << endl;
}
else
if (opt == 2)
{
cout << "Please input value:\n";
cin >> a;
Fibonacci_I(a);
}
system("pause");
return 0;
}
When implementing an algorithm, I like to mentally evaluate the algorithm, to see what the computer is doing.
fib(30)
= fib(29) + fib(28)
= fib(28) + fib(27) + fib(27) + fib(26)
= fib(27) + fib(26) + fib(26) + fib(25)+ fib(26)+ fib(25) + fib(25)+ fib(24)
I notice that this algorithm seems really really inefficient.
To calculate fib(30) it require 2^30 calculations! That's 1073741824!
Even worse, if I increase the number by 1, the time/operations to get a result doubles!
It will probably run forever with a large enough number! (Well until I fall asleep or my CPU burns up atleast)
Optimization
However, I notice that a lot of the calculations are redundant. To calculate fib(30), it must calculate fib(29) and fib(28). To calculate fib(29), it calculates fib(28) again. So inefficient!
When I see these kind of calculations the first thing that springs to mind is a technique call memoization. It caches results from a previous calculation and stores them for later use.
Using that in this case will reduce the number of calculations to something around 60 operations (?) at the cost of using more memory.
Example: What is memoization and how can I use it in Python?
It is not stuck, it just takes too long. It is because the complexity of recursive calculation. I assume that this school example was meant to show why recursive approach is inferior most of the time and should not be used.
Suppose you want to calculate Fibonacci_R(10). You will need to call Fibonacci_R(8) and Fibonacci_R(9). For calculation of Fibonacci_R(9) you will need to call Fibonacci_R(8) and Fibonacci_R(7). Which means you are calling Fibonacci_R(8) twice. If you continue along you will find that you are calling Fibonacci_R(7) three times - two times for Fibonacci_R(8) and once for Fibonacci_R(9)... you call Fibonacci_R(6) five times - two times for Fibonacci_R(8) and three times for Fibonacci_R(7)... etc. This is really hard when you start not with 10 but with 30. This will make your processor burn if you increase the number even more. Do not try it at home or make sure you have fire extinguisher at hand.
Btw. recursive algorithm is usually inferior not because of the complexity but because of limited memory stack which is used to store the variable and return address when calling a function.
A condition needed to be set for
"int a"
so while true, the "Fibunacci_R" function could execute. So;
I used a "do/while" condition/loop at the point where "Fibunacci_R" is called and it worked
#include <iostream>
using namespace std;
/* Fibonacci: recursive version */
int Fibonacci_R(int n)
{
if (n <= 0) return 0;
else if (n == 1) return 1;
else return (Fibonacci_R(n - 1) + Fibonacci_R(n - 2));
}
// iterative version
int Fibonacci_I(int n)
{
int fib[] = { 0, 1, 1 };
for (int i = 2; i <= n; i++)
{
fib[i % 3] = fib[(i - 1) % 3] + fib[(i - 2) % 3];
cout << "fib(" << i << ") = " << fib[i % 3] << endl;
}
return fib[n % 3];
}
int main() {
int a, opt;
cout << "Please choose the available option:\n";
cout << "1)Recursive\n";
cout << "2)Iterative\n";
cin >> opt;
if (opt == 1) {
cout << "Please input value:\n";
cin >> a;
// Here
do {
cout << endl << "From recursive function" << endl;
for (int i = 1; i <= a; ++i)
cout << "fib(" << i << ") = " << Fibonacci_R(i) << endl;
cout << endl;
break;
} while (Fibonacci_R(a));
}
else
if (opt == 2)
{
cout << "Please input value:\n";
cin >> a;
Fibonacci_I(a);
}
system("pause");
return 0;
}
It just takes an awful lot of time to calculate an input of integers greater than 45
int main()
{
int theArray [20] = {0};
int userInput = 0;
int populateCount = 0;
cout << "Enter 20 integers between 10 and 100 inclusive. " << endl;
while (populateCount < 20)
{
cin >> userInput;
theArray[populateCount] = {userInput};
if (userInput<10||userInput>100)
{
cout << "That is not a legal input. " << endl;
populateCount - 2;
}
else
{
populateCount++;
}
}
cout << "\n";
for (int i = 0; i < 20; i++)
{
cout << theArray[i] << endl;
}
}
I've got the baseline of my code done. The user enters twenty numbers and they're added to the array. If it's less than 10 or greater than 100 it's not a legal input, I subtract from the count, and they're allowed to go again. Then after the user finishes plugging in numbers it prints the array. However, I've been trying different if statements inside the array to eliminate duplicates, such as (if theArray[i] == theArray[i+1] then [i+1] = 0) I suppose that could work if I incorporated a sort to get all the 0's at the end, but is there a more efficient way to do this?
Before I go to the answer I suggest we clean it up slightly to make the problem more clear and remove other confusion.
Misconception
The statement populateCount - 2 has no effect.. instead you are simply not incrementing populateCount which is why the loop doesn't advance.
I would suggest something of this format within the loop. It puts the 'happy' path first, which will also make for some clearer ways to handle the second part.
if (userInput >= 10 && userInput <= 100 ) {
theArray[populateCount++] = userInput;
}
else {
std::cout << userInput << " is not legal input, must enter value "
<< "between 10 and 100. " << std::endl;
}
Preface
Before we attack the problem first let's refactor so that we can break it down to a single function so that as we work we don't disturb everything else as well as gain flexibility for testing and simplify readability.
Refactor
/* this code is responsible for doing what is needed to
* only insert unique items */
bool insert( const int& input, int* array, int num_elements ) {
// the fun part!
// more to follow shortly
};
/* gets user input forcing input to be between min and max */
int getUserInput(int min, int max) {
bool found = false;
int result = 0;
/* this can be done with less code but this makes it easy
* to see whats going on */
while ( !found ) {
cout << "Please enter a value between " << min << " and " << max << "." << endl;
cin >> result;
if ( result >= min && result <= max ) {
found = true; //yes we could break or return from here
} else {
cout << result << " invalid. "<< endl;
}
}
return result;
};
void printArray( const int* array, const int& size ) {
for (int i = 0; i < size; i++)
{
cout << array[i] << endl;
}
};
int main()
{
const int totalElements = 20;
int theArray [totalElements] = {0};
int userInput = 0;
int populateCount = 0;
int minVal = 10;
int maxVal = 100;
cout << "Enter " << totalElements << " integers between "
<< minVal << " and " << maxVal << " inclusive. " << endl;
while ( populateCount < numElements )
{
//this will percievably loop until it's good
userInput = getUserInput(minVal, maxVal);
if ( insert( userInput, theArray, populateCount ) )
++populateCount; //increments if we did insert it
}
}
cout << endl;
printArray( theArray, totalElements );
}
Attacking the problem
Ok so now our problem is simple, we just have to write the insert function. There are a couple of choices here, you can check each element in turn which as you said can be slow, O(n), or we could sort the array to make it quick, O(log n) + cost of sorting. Other possibilities I presume aren't available are using a std::set instead of an array, or using STL to do the work of sorting and finding. Note that in these modes insert won't actually do an insertion if the number is already present.
Another unique idea is to use an array of bools size max-min, and simply flag the index of input-min as true when you find it. This will be fast at the cost of size depending upon the gap between min and max. (this is essentially a hash function)
The advantage we are at from a refactor is that you can in turn write and try each of these solutions and even feed them the same canned input now that we've refactored so that you can try and time each one. For timing I would heavily suggest you add lots of numbers and consider greatly expanding the min and max to understand the scalability of each choice
You may have heard of a website called Project Euler (projecteuler.net). I'm working through the first problems, which were quite trivial, and I'm on the problem described in the title.
This isn't about optimising or anything - it takes about 90 thousandths of a second to complete. It's giving me the wrong total.
Can someone help me? I have no clue why the answer I'm getting - from both the array total (atotal) and the total that was added up normally (total) - is incorrect. The answer they are both showing is 947402457, which the website it telling me is the wrong answer.
In case it's just the wording, the question is here: http://projecteuler.net/index.php?section=problems&id=10
What's also very strange is, as far as I can tell, when, at the end when you can type in which prime number you would like to view (it takes it out of the array), it gives you the correct answer.
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
typedef unsigned long int bignum;
//there are 666671 primes below two million
int main(){
using namespace std;
bignum top = 2000000;
bignum total = 0;
bignum atotal = 0;
//hardcode 2 and 3
total += 5;
int inc = 2;
bignum n = 5;
double sq = n;
bignum np = 1;
bignum *pa = new bignum[top];
pa[0] = 2;
pa[1] = 3;
while (n < top){
int div = 5;
int divinc = 2;
int p = 1;
//check if number is prime
//check divisiblity from any possible prime up to the square root of n
//first hardcode 2 and 3
if(n%2==0||n%3==0)
p = 0;
else{
while(div<=sqrt(sq)){
if(n%div==0){
p = 0;
break;
}else{
div = div + divinc;
if(divinc==2) divinc = 4; else divinc = 2;
}
}if(p!=0){ //if it's a prime - 0 is not, 1 is prime
total = total + n;
np++;
pa[np] = n;
//cout << np << " prime number: " << n << endl; //takes too long if it prints everything
}
}
n += inc;
if(inc==2) inc = 4; else inc = 2;
}
for (int c=0;c<=np;c++){
atotal += pa[c];
}
cout << "Total " << top << ": " << total << endl;
cout << "Array total: " << atotal << endl;
cout << "Elapsed time: " << clock() << " " << CLOCKS_PER_SEC << "s of a second" << endl << endl;
while(true){
int ptoview = 0;
cout << "Enter the number of the prime you would like to see (you can view every prime number below "<<top<<") ";
cin >> ptoview;
if (pa[ptoview-1]){
if (pa[ptoview-1] < top)
cout << ptoview << " prime: " << pa[ptoview-1] << endl;
else
cout << "Too high/low" << endl;
cout << endl;
}
}
system("PAUSE");
return 0;
}
Here's a clue to at least one problem. Have a look at what happens when you replace:
for (int c=0;c<=np;c++){
atotal += pa[c];
}
with:
for (int c=0;c<=np;c++){
bignum oldatotal = atotal;
atotal += pa[c];
if (atotal < oldatotal)
cout << "Hmmm: " << oldatotal << " " << atotal << endl;
}
I get something like:
Hmmm: 4294819625 12858
Hmmm: 4294864122 123849
Hmmm: 4294717053 27802
Hmmm: 4294697657 51420
: : :
Hmmm: 4293781002 792849
Hmmm: 4294658253 1676602
Hmmm: 4293686116 710941
Hmmm: 4294706293 1737578
Total 2000000: 947402457
Array total: 947402457
I won't go into the detail since this is a puzzle and I'm assuming you want to keep it at least a little challenging :-)
And yes, you're right (based on your comment below) so I'll make the answer a little less obtuse so it's more useful for others.
The unsigned long type is not big enough to hold the sum of all those primes so it's wrapping around.
Whether it can hold the actual primes themselves I haven't checked, but the answer in the next paragraph will solve that as well.
You might want to try redefining bignum as a "larger" type like unsigned long long if available.
Not looked at everything but sq isn't modified in the main while loop. That doesn't seem right. (BTW, I'd have used a sieve filter to get to the primes).