Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7631973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:29:50+00:00 2026-05-31T06:29:50+00:00

I have written a code which calculates Fibonacci number using my own arbitrary precision

  • 0

I have written a code which calculates Fibonacci number using my own arbitrary precision functions for multiplication and addition.

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>

using namespace std;

#define max 3000

struct data_type{
   int string[max]; 
};

struct data_type mul(struct data_type a,struct data_type b) {
   struct data_type c;
   memset(c.string,0,sizeof(int)*max);
   int k;
   int i,j;
   for(i=max-1;i>0;i--) {
      int num=a.string[i];
      int carry=0;
      int rem=0;
      k=i;

      for(j=max-1;j>0;j--) {
         int n=b.string[j]*num + carry;
         int r=n%10;
         carry=n/10;
         int p=(c.string[k]+r+rem)%10;
         rem=(c.string[k]+r+rem)/10;
         c.string[k]=p;  
         k--;
      }
   }

   return c;
}

struct data_type add(struct data_type a,struct data_type b) {
   struct data_type c;
   memset(c.string,0,sizeof(int)*max);
   int carry=0;
   int i,j;
   int k=max-1;

   for(i=max-1,j=max-1;i>0 && j>0;i--,j--) {
      int res=(a.string[i] + b.string[j])+carry;
      carry=res/10;
      c.string[k--]=res%10;
   }

   return c;
}

void multiply(struct data_type f[2][2],struct data_type m[2][2]){

   struct data_type x,y,z,t;
   x=add(mul(f[0][0],m[0][0]),mul(f[0][1],m[1][0]));    
   y=add(mul(f[0][0],m[0][1]),mul(f[0][1],m[1][1]));    
   z=add(mul(f[1][0],m[0][0]),mul(f[1][1],m[1][0]));    
   t=add(mul(f[1][0],m[0][1]),mul(f[1][1],m[1][1]));    

   f[0][0]=x;
   f[0][1]=y;
   f[1][0]=z;
   f[1][1]=t;       
}

void power(struct data_type f[2][2],unsigned long long n){

   if((n==0)||(n==1))
      return ;
   struct data_type a,b;
   memset(a.string,0,sizeof(int)*max);
   memset(b.string,0,sizeof(int)*max);
   a.string[max-1]=1;
   b.string[max-1]=0;
   struct data_type m[2][2]={{a,a},{a,b}};  

   power(f,n/2);

   multiply(f,f);

   if(n%2 != 0)
       multiply(f,m);
   }

   struct data_type fib(unsigned long long n){
   struct data_type a,b;
   memset(a.string,0,sizeof(int)*max);
   memset(b.string,0,sizeof(int)*max);
   a.string[max-1]=1;
   b.string[max-1]=0;

   struct data_type f[2][2]={{a,a},{a,b}};

   if(n==0)
      return b;

   power(f,n-1);
   return f[0][0];
}

int main()
{   
   int t;
   cin>>t;

   for(int w=0;w<t;w++){
      unsigned long long n;
      cin>>n;
      struct data_type a,b,c;

      a=fib(n-1);
      b=fib(n+2);
      c=add(a,b);

      int ck=0;

      for(int i=0;i<max;i++) {
         if(c.string[i] && !ck)
            ck=1;

         if(ck)
            cout<<c.string[i];
      }

      cout<<"\n";
   }

   return 0;
}

My problem is that it’s showing SIGSEGV on running but sometimes runs correctly say for example I have tested it on fib(50000) for max=4000 but as soon as I change max=5000 it stops working. I’m breaking my head for hours but still unabled to figure out the problem.

Please tell me what might be possibly wrong with my code.

Is there any logical mistake in the code which is causing runtime error ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:29:51+00:00Added an answer on May 31, 2026 at 6:29 am

    I think the problem is in the mul() function as the index k in the inner for loop can become a negative integer and k is used to index an array.

    k is decremented max - 1 times always but it is only set to max - 1 on the first iteration of the outer for loop. In subsequent iterations of the outer for loop it is set to a value less than max - 1 resulting in k becoming negative.

    EDIT:

    I modified the inner for loop:

    for(j=max-1;j>0;j--){
        if (k < 0)
            std::cout << k << "\n";
        ...
    }
    

    and supplied 1 and 4 when prompted during execution and many negative values were displayed: so this is definitely one problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
I have some code I've written in PHP for consuming our simple webservice, which
I have one C code app. which i was building using MS-VS2005. I had
I have some delphi code which, given a list of items, calculates the total
I have written code which saves a user's answers as well as a recorded
I have written this code which accesses the Delete method on Web Service x,
I have written a code which makes a border of a div orange then
I have written the code which downloads the file from FTP server. Since I
i have written a code which converts date and time into timezone , now
I have written following code which generates a browse and upload button.i want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.