Checking whether numbers belong to a Lucas sequence

I am new to C++ and this is the second code that I have ever written.
I have the first part of my code which checks if a number belongs to the Lucas sequence:

2, 1, 3, 4, 7, 11, 18, ...

When I just check once or in increasing order, it works , but if I keep checking, and eventually I check a number that I already checked before, then it gives me a wrong result.

So for instance, when I check the numbers (in this order):

4; 7 and 18, it says that they are all Lucas numbers.

But when I check 18 first (it says that 18 is a Lucas number), and then when I check 4 or 7, the program tells me that they are NOT Lucas numbers.

Any idea on how to fix this?

Here is the first part of my code:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<cmath>
#include<cstdlib>
#include <ctype.h>


using namespace std;
int main(void)
{
     //Variables declarations
    int L1=2, L2=1, L3=3 ;
    int choice, num ;
    char q;
    
    do{//Executable code
    
       
    cout<<"Lucas Numbers Calculator"<< endl;
    
    cout<< "Please enter one of the following choices. " << endl;
    cout<<"1. To find if a number belongs to the series or not."<<endl;
    cout<<"2. To find the n-th number in rank."<<endl;
    cout<<"3. To compute the sum of all Lucas numbers up to a given bound."<<endl<<endl;
  
    cout<<"Please choose: ";
    cin>> choice;
    
    
    if (choice !=1  && choice!=2 && choice!=3){
       cout<< "Wrong choice. Please choose again."<<endl << endl ;
       }
    else if (choice==1) {
       cout<<"Please give the number you want to check: ";
       cin>> num; 
       
       if (!(num > 0)){
          cout<< "Only positive numbers are accepted."<< endl<<endl ;
          }
       else if (num==2 || num==1 || num==3) {
                cout<< num << " IS a Lucas number."<<endl<<endl;
               }
             
               
       else if (num >3) {
                
            while (num > L3){
               L3=L1+L2;
               L1=L2;
               L2=L3;
                }
           
            if (num==L3) {
                cout<< num << " IS a Lucas number."<<endl<<endl;}
               
            else  {
                cout<< num << " is NOT a Lucas number."<< endl<<endl; }  
                 
                         } //  end  else if (num >3)
    
    } // end choice ==1          
                
            
            
     }while (choice != 0);
            
              
    
    system("PAUSE");
    return 0;
    
 }


Most likely you need to put the initialisation of L1, L2 and L3 inside the do-while loop. Otherwise, after the first time around, these variables will contain whatever values were stored as a result of a previous calculation.
Last edited on
Thanks, it worked , I put

L1=2;
L2=1;
L3=3;

inside the part

1
2
3
4
5
6
7
  else if (num >3) {
                
            while (num > L3){
               L3=L1+L2;
               L1=L2;
               L2=L3;
                }


right before while (num > L3).
Yes, that makes sense.
Topic archived. No new replies allowed.