🔒 Closed Fibonacci in tagalog

Status
Not open for further replies.

codyscott

Honorary Poster
Parang ganito lang yata ang Fibonacci.
Sinubukan ko i-explain. Tignan nyo na lang kung papatok.
Tumakbo naman at mukhang tama. ;)

CODE WITH EXPLANATION
Code:
/*
Topic: Fibonacci
Sample: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34........and so on!
Operation: 0+1=1, 1+1=2, 1+2=3, 2+3=5, 3+5=8, 5+8=13, 8+13=21, 13+21=34.......and so on!
Author: Codyscott at phcorner.org
Group: Seriously Java (mga 'tol, sali kayo)
*/

#include <iostream>
using namespace std;

int main() {
    //1. sabihin natin kung ilang sequence ang gusto nating makita na result (optional).
    //pero kung walang ilalagay, magiging infinite siya (meaning hanggang kaya ng computer mo)
    int numberOfSequence = 0;
    cout << "Ilang sequence ang gusto mong makita sa Fibonutz mo?";
    cin >> numberOfSequence;
 
    //2. Ang Fibonacci ay laging i-a-add niya the PREVIOUS number + the NEW result
    // Parang ganito: 2, 3 = 5
    // Tapos sa next line magiging 3, 5 = 8
    // tapos sa next line magiging, 5, 8 = 13....and so on!
    // ang magiging itsura na niya ay 0, 1, 1, 2, 3, 5, 8, 13, 21, 34........and so on!
    // so laging DALAWANG value ang i-a-add kaya gawa tayo ng array na may dalawang spots.
    int pair[2];
 
 
    //3. Tatanungin natin ang user kung saan niya gustong mag simula (puwedeng 0, puwede rin kahit anong number, basta integer lang).
    int firstEntry = 0;
    cout << "Saan mo gusto magsimula ang Fibonutz mo?";
    cin >> firstEntry;
 
    //4. Iva-validate lang natin yung entry ng user para ang sa print out para
    //mai-print natin yung buong sequence
    //Kung ZERO ang inilagay, i-pre-pare natin yung array na ganito
    if(firstEntry==0){
        pair[0] = firstEntry;
        pair[1] = firstEntry+1;
    //Kung hindi zero ang inilagay, i-prepare natin yung array na ganito
    } else {
        pair[0] = 0;
        pair[1] = firstEntry;
    }
 
    //5. gawa tayo ng placeholder ng total ng dalawang i-a-add natin na values
    int result = 0;
 
    //6. I-pri-print lang natin yung na-validate na na user entry (para lang iprint para makita ang buong sequence)
    cout << pair[0] << ", " << pair[1] << ", ";
 
    //7. Ito na yung meat ng app.
    //**********************************************
    //ito yung gagawa ng Fibonnaci
    for(int i=0; i<numberOfSequence; i++){
        //Kada isang loop
        //7a. unang task ay kunin ang first and second values sa pair array ng ganito:
        int first = pair[0];
        int second = pair[1];
     
        //7b. susunod na task ay i-add ang dalawang value
        result = first+second;
     
        //7c. susunod na task ay i-print
        cout << result << ", ";
     
        //7d. final task, importante
        //i-update or baguhin na yung laman ng pair array ng ganito:
        //wala na yung dating number 1, tapos yung dating number 2 ay magiging number 1. Tapos yung
        //bagong result ay magiging number 2 na sa pair array
        pair[0] = second;
        pair[1] = result;
     
        //tapos dito, loop or balik uli siya sa step 7a.
        //at gagawin niya ito hanggang matapos niya ang sinabi ng user na kung ilang sequences.
    }
    //**********************************************
 
}

CODE WITHOUT EXPLANATION
Code:
/*
Topic: Fibonacci
Sample: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34........and so on!
Operation: 0+1=1, 1+1=2, 1+2=3, 2+3=5, 3+5=8, 5+8=13, 8+13=21, 13+21=34.......and so on!
Author: Codyscott at phcorner.org
Group: Seriously Java (mga 'tol, sali kayo) 
*/
#include <iostream>
using namespace std;
int main() {
    int numberOfSequence = 0;
    cout << "Ilang sequence ang gusto mong makita sa Fiboutz mo?";
    cin >> numberOfSequence;
    int pair[2];
    int firstEntry = 0;
    cout << "Saan mo gusto magsimula ang Fibonutz mo?";
    cin >> firstEntry;
   
    if(firstEntry==0){
        pair[0] = firstEntry;
        pair[1] = firstEntry+1;
   
    } else {
        pair[0] = 0;
        pair[1] = firstEntry;
    }
   
    int result = 0;
    cout << pair[0] << ", " << pair[1] << ", ";
   
    for(int i=0; i<numberOfSequence; i++){
        int first = pair[0];
        int second = pair[1];
        result = first+second;
        cout << result << ", ";
        pair[0] = second;
        pair[1] = result;
    }   
}
 
sir mali ata formula mo sa first entry, pag 3 po kasi ang first entry mali na ang sequence nya,
ang 10th term po kasi ng fibonacci is 55 meaning pag 3 nilagay mo sa first entry dapat ang sequence is 2, 3, 5, 8, 13, 21, 34, 55 . . .
 
sir mali ata formula mo sa first entry, pag 3 po kasi ang first entry mali na ang sequence nya,
ang 10th term po kasi ng fibonacci is 55 meaning pag 3 nilagay mo sa first entry dapat ang sequence is 2, 3, 5, 8, 13, 21, 34, 55 . . .
baka....pero...hindi ba dapat pag 3, ganito lang lalabas:
0,3,3,6,9,15,24,39,63

pag 1 naman nagsimula
0,1,1,2,3,5,8.....

pag 5 naman nagsimula
0,5,5,10,15,25....

- ganyan ang intindi ko sa fibonacci....but...i could be wrong :)
 
sir mali ata formula mo sa first entry, pag 3 po kasi ang first entry mali na ang sequence nya,
ang 10th term po kasi ng fibonacci is 55 meaning pag 3 nilagay mo sa first entry dapat ang sequence is 2, 3, 5, 8, 13, 21, 34, 55 . . .

yes, tama ka nga. Mali ako at mali pagkaka intindi ko. Thank you.
- fibonacci starts with either 0 or 1...and..followed by one....

in this case, pakita mo kung ano dapat babaguhin sa code... for everybody's learning purposes :)
 
Code:
/*
Topic: Fibonacci
Sample: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34........and so on!
Operation: 0+1=1, 1+1=2, 1+2=3, 2+3=5, 3+5=8, 5+8=13, 8+13=21, 13+21=34.......and so on!
Author: Codyscott at phcorner.org
Group: Seriously Java (mga 'tol, sali kayo)
*/
#include <iostream>
using namespace std;
int main() {
    int numberOfSequence = 0; //ito po ang magiging number of output
    cout << "Ilang sequence ang gusto mong makita sa Fiboutz mo?";
    cin >> numberOfSequence;
    int pair[2];
    int firstEntry = 0; // ung firstEntry ay magiging term number kng saan ka mag sisimula mag output
    cout << "Saan mo gusto magsimula ang Fibonutz mo?";
    cin >> firstEntry;
  
        pair[0] = 0;
        pair[1] = 1;
  
  
    int result = 0;
    numberOfSequence = numberOfSequence + firstEntry; //factoring ng sequence
    for(int i=0; i<numberOfSequence; i++){
        int first = pair[0];
        int second = pair[1];
        result = first+second;
      if(i >= firstEntry){    //ito po ang control ng output
              cout << result << ", ";
        }
        pair[0] = second;
        pair[1] = result;
    }   
}

yan po ay based sa fibonacci formula Xn = Xn-1 + Xn-2 where n is the term number meaning kng gusto mong malaman ang 8th term ng fibonacci sequence ang formula ay
X8 = X7 + X6
X8 = 13 + 8
X8 = 21

P.S.
: hangang 96th term lng ng febonacci ang kayang eh output nyan pag pinalitan ng long datatype. d ko alam kng pano nila na kuha ang first 500 term ng febonacci na kita ko lng sa google.
 
Code:
/*
Topic: Fibonacci
Sample: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34........and so on!
Operation: 0+1=1, 1+1=2, 1+2=3, 2+3=5, 3+5=8, 5+8=13, 8+13=21, 13+21=34.......and so on!
Author: Codyscott at phcorner.org
Group: Seriously Java (mga 'tol, sali kayo)
*/
#include <iostream>
using namespace std;
int main() {
    int numberOfSequence = 0; //ito po ang magiging number of output
    cout << "Ilang sequence ang gusto mong makita sa Fiboutz mo?";
    cin >> numberOfSequence;
    int pair[2];
    int firstEntry = 0; // ung firstEntry ay magiging term number kng saan ka mag sisimula mag output
    cout << "Saan mo gusto magsimula ang Fibonutz mo?";
    cin >> firstEntry;
 
        pair[0] = 0;
        pair[1] = 1;
 
 
    int result = 0;
    numberOfSequence = numberOfSequence + firstEntry; //factoring ng sequence
    for(int i=0; i<numberOfSequence; i++){
        int first = pair[0];
        int second = pair[1];
        result = first+second;
      if(i >= firstEntry){    //ito po ang control ng output
              cout << result << ", ";
        }
        pair[0] = second;
        pair[1] = result;
    }  
}

yan po ay based sa fibonacci formula Xn = Xn-1 + Xn-2 where n is the term number meaning kng gusto mong malaman ang 8th term ng fibonacci sequence ang formula ay
X8 = X7 + X6
X8 = 13 + 8
X8 = 21

P.S.
: hangang 96th term lng ng febonacci ang kayang eh output nyan pag pinalitan ng long datatype. d ko alam kng pano nila na kuha ang first 500 term ng febonacci na kita ko lng sa google.
i guess then ang fibonacci is a fixed sequence.
ibig sabihin hindi na kailangan tanungin ang user kung saan magsisimula (otherwise, masisira ang sequence)
- ang puwedeng tanungin lang ay kung ilang sequences ang gustong makita.

I think 500 terms ay over the maximum of any number data type. Ang naiisip ko na workaround is using concatenation (pang daya lang) since screen output lang naman ang kailangan.

:)
 
Status
Not open for further replies.

About this Thread

  • 8
    Replies
  • 2K
    Views
  • 4
    Participants
Last reply from:
codyscott

Online now

Members online
390
Guests online
1,584
Total visitors
1,974

Forum statistics

Threads
2,275,513
Posts
28,963,776
Members
1,232,826
Latest member
Daniella
Back
Top