Example #1
0
int Solution::fib_iter(int a,int b,int count){
  if (count == 0) {
    return b;
  } else {
    return fib_iter(a + b, a, count - 1);
  }
}
Example #2
0
void BM_fib_iter(benchmark::State& state) {
    const auto n = state.range_x();

    while (state.KeepRunning()) {
        auto ret = fib_iter(n);
        benchmark::DoNotOptimize(ret);
    }
}
Example #3
0
void recursion(){
	/* ----------------Part 4---------------------*/
	//Fib Variables
	int fibNum = 0;
	cout << "Which number of the Fibonnaci Sequence do you want? \n(For this program, we are NOT including 0 as a part of the sequence.)\n";
	cin >> fibNum;
	cout << "That number is " << fib(fibNum) << endl; //original call

	//Pow Variables
	int base = 0,  power = 0;
	cout << "Moving on to power. What number do you want raised to what power\n(use format [number space power])" << endl;
	cin >> base >> power;
	cout << "That comes out to " << pow(base, power) << endl; //original call

	//Tri Variables
	int triNum = 0;
	cout << "Moving on to tri. Which triangular number do you want?\n";
	cin >> triNum;
	cout << "That comes out to " << tri(triNum) << endl << "Your triangle looks something like this:\n"; //original call
	drawTri(triNum);  // random function I wanted to do, because why not?

	//Gcd Variables
	int gcdNum1, gcdNum2;
	cout << "Finally, moving on to gcd. Please enter the two numbers you want \n to find the greatest common denominator of. \n(Use formate [number1 space number2]) \n";
	cin >> gcdNum1 >> gcdNum2;
	if (gcdNum1 < 0 || gcdNum2 < 0){
		gcdNum1 = abs(gcdNum1);
		gcdNum2 = abs(gcdNum2);
	}
	cout << "The greatest common denominator between these two numbers is:\n" << gcd(gcdNum1, gcdNum2) << endl; // original call

	cout << "Now moving on to iterating." << endl;

	/* ----------------Part 5---------------------*/
	//Fib Variables
	int fibNum_iter;
	cout << "Which number of the Fibonnaci Sequence do you want? \n(For this program, we are NOT including 0 as a part of the sequence.)\n";
	cin >> fibNum_iter;
	cout << "That number is " << fib_iter(fibNum_iter) << endl;

	//Pow Variables
	int base_iter, power_iter;
	cout << "Moving on to power. What number do you want raised to what power\n(use format [number space power])" << endl;
	cin >> base_iter >> power_iter;
	cout << "That comes out to " << pow_iter(base_iter, power_iter) << endl;

	//Tri Variables
	int triNum_iter;
	cout << "Moving on to tri. Which triangular number do you want?\n";
	cin >> triNum_iter;
	cout << "That comes out to " << tri_iter(triNum_iter) << endl;

	//GCD Variables
	int gcdNum1_iter, gcdNum2_iter;
	cout << "Finally, moving on to gcd. Please enter the two numbers you want \n to find the greatest common denominator of. \n(Use formate [number1 space number2]) \n";
	cin >> gcdNum1_iter >> gcdNum2_iter;
	cout << "The greatest common denominator between these two numbers is:\n" << gcd_iter(gcdNum1_iter, gcdNum2_iter) << endl;
}
Example #4
0
void taskMain(void *raw_args) {
    char **argv = raw_args;
    const int n = atoi(argv[1]);
    const int doDDT = argv[2] && atoi(argv[2]);
    const long fn = fib_iter(n);
    const long fnp1 = fib_iter(n+1);
    long answer;
    double t_start, t_end;
    // ASYNC-FINISH version
    if (!doDDT) {
        printf("async/finish version\n");
        t_start = get_seconds();
        FibArgs args = { n, 0 };
        FINISH {
            hclib_async(fib, &args, NO_FUTURE, NO_PHASER, ANY_PLACE, NO_PROP);
        }
        t_end = get_seconds();
        answer = args.res;
        //printf("asyncs = %ld\tfins=%ld\n", 2*fnp1-1, fnp1);
    }
Example #5
0
TEST(fib_iter, basic) {
    ASSERT_EQ(0, fib_iter(0));
    ASSERT_EQ(1, fib_iter(1));
    ASSERT_EQ(1, fib_iter(2));
    ASSERT_EQ(2, fib_iter(3));
    ASSERT_EQ(3, fib_iter(4));
    ASSERT_EQ(5, fib_iter(5));

    ASSERT_EQ(55u, fib_iter(10u));
}
Example #6
0
T fib_mem(const T& n) {
    static_assert(std::is_integral<T>::value);
    static std::unordered_map<T, T> lookup_table;

    auto it = lookup_table.find(n);
    if (it == lookup_table.end()) {
        // Compute value:
        T res{fib_iter(n)};
        lookup_table.emplace(n, res);
        return res;
    } else {
        // Fetch from lookup table.
        return (*it).second;
    }
}
Example #7
0
//
// Specialisation for int memoisation. Using a flat array instead of a
// hash table, since we can bound the valid sizes of .
//
int fib_mem(const int& n) {
    const size_t lookup_table_size = 47;
    const auto max_idx = static_cast<int>(lookup_table_size) - 1;

    static int lookup_table[lookup_table_size] = {};

    if (n < -max_idx && n > max_idx)
        throw std::out_of_range("lookup_table");

    const auto idx = abs(n);
    if (!lookup_table[idx])
        lookup_table[idx] = fib_iter(n);

    return lookup_table[idx];
}
Example #8
0
File: prog.c Project: dgemm/ioc1
void test(void)
{
  const int max = 20000;

  for (int kk = 0; kk <= max; kk++) {
    const long_t result_iter = fib_iter(kk);
    const long_t result_g = fib_g(kk);

    // How far to overflow?
    const double frac = ((double) result_iter)/long_t_max;

    printf("%d: %llu, %llu (%.10lf)\n", kk, result_iter, result_g, frac);

    assert(result_iter == result_g);
  }
}
Example #9
0
int fib_iter(int n) //recursive case
{
	if (n == 0 || n == 1)
		return n;
	return fib_iter(n - 1) + fib_iter(n - 2);
}
Example #10
0
int Solution::fibonacci(int n){
  return fib_iter(1, 0, n - 1);
}