Ive been told to do a program that prints the first 15 non-fibonacci series using 'while'...i got the fibonacci series correct, but cant find a solution for this...
Here is my code for printing the first 15 fibonacci series...

#include<iostream.h>
#include<conio.h>
void main()
{
  int n=1;
  int a=-1;
  int b=1;
  int c;
  cout<<"Fibonacci series (first 15 values)\n";
  while(n<=15)
  {
     cout<<"\n"<<a+b;
     c=a+b;
     a=b;
     b=c;
     n=n+1;
  }
  getch();
}

Recommended Answers

All 5 Replies

Then go for, Lucas series. The only difference is the starting value.
For fibonacci f(0) = 1, and f(1) = 1. Then its defined recursively as
f(n) = f(n-1) + f(n-2), where n >=2.

For Lucus series, f(0) = 2, and f(1) = 1, then its defined recursively as
f(n) = f(n-1) + f(n-2) , where n >=2.

As you see they are very similar, yet different. So all you need
to do is change your starting values.

For example you can do something like this to generate fibbonicci
numbers

//initial value
	const int F_0 = 0;
	const int F_1 = 1;

	const int UPTO = 20;
	
	int currNum = F_0;
	int nextNum = F_1;

	int result = 0;

	for(int i = 0; i < UPTO; ++i){
		result = currNum + nextNum;
		cout << "fib(" << i <<") = " << result << endl;		
		currNum = nextNum;
		nextNum = result;
	}

Now if you change F_0 = 2 and F_1 = 0, then you generate the lucus number. The main point here is to see that fibonacci numbers
rely not only on their recursive definition but also the initial condition.
You don't have to generate the Lucus numbers, just realize that
by changing the initial value, or the starting value, you generate
your own set of sequence, call it aswin sequence, long as its not already
founded.

Oops...sorry, guess i didnt write what i wanted properly!:D .I actually wanted the first 15 non-fibonacci numbers...
Like for fibonacci, you have-0,1,1,2,3,5,8,13...
non-fibonacci-4,6,7,9,10,11,12...(the nos that come in between fibonacci numbers)

Once you have a new Fibonacci number use another small loop to go from the previous Fib number until the current one. Record/output those values that you find.

well you have found the fibonaci series of numbers with a formula

if you place this code into a function to find the fibonaci numbers.

Here are two approaches:

1 - write a function that stores all of the fibonaci numbers between
a lower and upper limit.

this would best be achieved with a vector

2 - write an method to check if a number is a fibonaci_number bool is_fib_num(int to_check); if you do a while loop like you have but check

bool is_fib_num(int num)
{
//in your function
ret = false;
   while(c < num)
  {
//add your existing code from your while loop here
     if(c == num)
    {
      ret =  true;
      break;
    }
 }
}
return ret;

now you need to go through main to check each number until you
have 15

int n = 0;
int num = 0;
while(n < 15)
{
   if(false == is_fib_num(num))
   {
     //not a fibonaci
    cout << num << endl;
    ++n;
  }
++num;
}

I will suggest the following :

1) Create a vector
2) Fill the vector with numbers 0 through MAX
3) Find one fib number from the vector and delete it.
4) Find the next fib number and delete it.
5) Repeat 3-4 until MAX is reached
6) Print the vector

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.