function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sourav046sourav046 

Fibonacci series on custom object (!)

I am given a work in which I have a custom object named Fibonacci .I have to write a class and associate the same with a page so that I can take the length of Fibonacci series from the page .Thats ok .Problem is that each Numbers coming in Fibonacci series should be inserted as a record in the Fibonacci object .Help with code please .

Thanks !

Best Answer chosen by Admin (Salesforce Developers) 
sourav046sourav046

Hi Sdry !

 

Thanx for your support .By the way I found the solution what I wanted .

public class InsertFibo{
   
   
    public  void insertList(Integer n){
        List<Fibonacci__c> tmplist=new List<Fibonacci__c>();
        Integer next;
        Integer first=0;
        Integer second=1;
        for(Integer i=0;i<n;i++){
            Fibonacci__c obj=new Fibonacci__c();
                next=first+second;
                obj.Name=string.valueof(next);
                tmplist.add(obj);
                first=second;
                second=next;    
        }
        insert tmplist;
    }
    
}

 

All Answers

SamuelDeRyckeSamuelDeRycke
No one is gonna do the work for you, but we may assist you in getting it done yourself. Specially not with unclear requirments or without knowledge of what you already have done yourself.

Where exactely are you stuck ? Do you know how to configure a custom object ? configure fields ? Are you unsure what fields you need ? Are you in doubt on how to store data into a custom object ? If you want us to help you .. you got to give us something more specific.

sourav046sourav046

Well.....the requirement is display each individual numbers of Fibonacci series as a Record of an custom object named FIBONACCI (which I have already created) .

 

and I am taking the length of the series by a parameter or from visualforce page(thats not my issue)

 

Problem is that when I am executing the method(or clicking the CommandButton ) I wish to see record NAMEs as 1,1,2,3,5,8,13,21..... in this order .

 

For convenience I assumed to map those number with [FIBONACCI].Name field but if you  suggest me to create another custom field in that object I will .

 

I know inserting a record by apex but I dont understand how to iterate the loop and insert every single numbers individually into the object .

 

I am sorry for providing opaque requirement I am almost new to apex development .Let me know if you need any additional info .

 

Thanks Sdry :)

SamuelDeRyckeSamuelDeRycke
So, if I get it right, you already have the numbers saves in fibonacci records in your salesforce organisation. But you have trouble displaying them on the page ? Ifso this should be fairly straight forward.

I see no issue with using the Name field as type text for your fibonacci values, You would have 1 record for each number.

Can you show me the code of your page & controller ? or any other code you have so far. It will be easier to help based on what you have.

sourav046sourav046
public class Fibonacci{
   
    public String lst;
    public Integer next;
   
    public Integer first=0;
    public Integer second=1;
    public String series(Integer n){
        for(Integer x=0;x<=n;x++){
            if(x<=1)
            {next=x;
            lst=String.valueOf(next+'\t');}
            
            else
           {next=first+second;
            lst+=String.valueof(next+'\t');
            first=second;
            second=next;
            }
        }
       return lst; 
        
    }

}

 This is my code to display Fibonacci series .But what I want is.....to display each number (e.g :1,1,2,3,5,8.....etc) to be a part of records of the FIBONACCI sObject (means there should be individual records named with those numbers or  there is another custom field in the FIBONACCI sObject in which I am displaying each numbers individually ) .I basically need to know how to use the insert() function here and map each numbers with the NAME field or with the Custom Sobject field .

 

Thanks and Regards !

sourav046sourav046

Hi Sdry !

 

Thanx for your support .By the way I found the solution what I wanted .

public class InsertFibo{
   
   
    public  void insertList(Integer n){
        List<Fibonacci__c> tmplist=new List<Fibonacci__c>();
        Integer next;
        Integer first=0;
        Integer second=1;
        for(Integer i=0;i<n;i++){
            Fibonacci__c obj=new Fibonacci__c();
                next=first+second;
                obj.Name=string.valueof(next);
                tmplist.add(obj);
                first=second;
                second=next;    
        }
        insert tmplist;
    }
    
}

 

This was selected as the best answer
megha agarwal 20megha agarwal 20

try this code for fibonacci series
Apex code:

public class Fibonacciseries {
    public string generatefibonacciseries(Integer num){
        String fibonacciseries='fibonacci series :';
        Integer f1=0, f2=1, nextnum=0;
        for(Integer i=0; i<=num; i++){
              nextnum=f1+f2;
            f1=f2;
            f2=nextnum;
            fibonacciseries +=','+String.ValueOf(nextnum);
        }
      
          return fibonacciseries;
    }
    
}
 Annoymous window:
Fibonacciseries f=new Fibonacciseries();
system.debug(f.generatefibonacciseries(8));
Arjun Gupta 75Arjun Gupta 75
There is small correction in the code, please keep comma(,) at the end of the display string so that for the iteration it will show.

public class Fibonacciseries {
    public string generateFibonacciseries(Integer n){
        String fibonacciseries='fibonacci series :'; 
        Integer f1=0, f2=1, nextnum=0;
        for(Integer i=0; i<=n; i++){
            nextnum=f1+f2;
            f1=f2;
            f2=nextnum;          
            fibonacciseries +=String.ValueOf(nextnum)+',';            
        }
      
          return fibonacciseries;
    }
    
}