Sort by

recency

|

711 Discussions

|

  • + 0 comments

    My solution Js, using memoization and closures.

    function fibonacciModified(t1, t2, n) {
        // Write your code here
        return memoized(t1, t2)(n);
    }
    
    const memoized = (t1, t2) => {
        const obj = { 1: BigInt(t1),
                      2: BigInt(t2) };
        return function fib(n) {
            if (obj[n]) {
                return obj[n]; 
            } else {
                if (n === 1) {
                    return BigInt(t1);
                } else if (n === 2) {
                    return BigInt(t2);
                } else {
                    obj[n] = BigInt(fib(n - 2)) + BigInt(fib(n - 1) ** BigInt(2));
                    return obj[n];
                }
            }
        } 
    # };
    
  • + 0 comments

    this problem can't be solved in c# unless you modify the return type, int can't handle the upper bound, so changed to string, used BigInteger internally and then it worked, this should be fixed in order to actually solve the problem, as it state "The function is expected to return an INTEGER." that is not valid

  • + 0 comments

    / /\ /\ /\

    public static BigInteger fibonacciModified(int t1, int t2, int n) { // Write your code here List results = new ArrayList<>(); results.add(BigInteger.valueOf(t1)); results.add(BigInteger.valueOf(t2)); BigInteger result = BigInteger.valueOf(0); for( int i =2;i

    }
    return results.get(results.size()-1) ;
    
    }
    
  • + 0 comments

    Kotlin O(n) time O(1) memory solution

    fun fibonacciModified(t1: Int, t2: Int, n: Int): BigInteger {
        if (n == 1) return t1.toBigInteger()
        if (n == 2) return t2.toBigInteger()
        var tminus2 = t1.toBigInteger()
        var tminus1 = t2.toBigInteger()
        var ti = BigInteger.ZERO
        for (i in 3..n) {
            ti = tminus2 + tminus1*tminus1
            tminus2 = tminus1
            tminus1 = ti
        }
        return ti
    }
    
  • + 2 comments
    // I store numbers as vectors. Any number can be represented as a vector of digits
    // [ a0 , .. , an] = a0 + a1*10 + .. + an*10^n
    // During calculations I temporarily do not use a constraint that each number a0 .. an is a single digit. I restore it after the summations and multiplications are done
    
    // Transform integer to vector
    vector<int> number_to_vector(int t)
    {
        vector <int> ans;
        
        while(t != 0)
        {
            ans.push_back(t % 10);
            t = int(t/10);
        }
        return ans;
    }
    
    
    
    
    
    // Compute the sum of two integers represented as vectors
    vector <int> my_sum_raw(const vector<int> &t1 , const vector<int> &t2)
    {
        int N1 = t1.size();
        int N2 = t2.size();
        int N = max(N1 , N2);
        
        int carry = 0;
        
        vector<int> ans (N);
        
        for (int i = 0 ; i < N ; i++)
        {
            int a = 0;
            int b = 0;
            
            if (i < N1)
                a = t1[i];
            if (i < N2)
                b = t2[i];
                
            ans[i] = a + b;
        }        
            
        return ans;
        
    }
    
    
    // The most brute-force way to compute the square of a number represented by a vector
    vector<int> my_square_2(const vector<int> &t)
    {
        vector<int> ans(2*t.size());
        
        int carry = 0;
        
        for(int i = 0 ; i < t.size() ; i++)
        {
    			  // Notice, that here I am avoiding double-counting. Otherwise my code would not pass all tests
            for(int j = i ; j < t.size() ; j++)
            {
                int x = t[i] * t[j];
                if (i != j)
                    x *= 2;
                ans[i+j] += x;
            }
        }
        
        
        
        return ans;
    }
    
    // Now I have got an answer represented as a vector [a0, .. , an], where the number is
    a0 + a1 * 10 +  ... + an * 10^n. I had abandoned the constraint of each number as a single digit. I restore it at the end.
    
    vector<int> clean_ans(const vector<int> &t)
    {
        vector<int> ans(t.size());    
        
        int a = 0;
        
        for (int i = 0 ; i < ans.size() ; i++)
        {
            a += t[i];
            
            ans[i] = a % 10;
            a = int(a/10);
            
        }
        
        
        while(a > 0)
        {
            ans.push_back(a % 10);
            a = int(a/10);
        }
        
        
        
        while (ans[ans.size()-1] == 0)
            ans.pop_back();
        
        return ans;
    
    }
    
    
    
    
    
    vector<int> fibonacciModified(int t1, int t2, int n) 
    {      
        vector<int> vect_t1 = number_to_vector(t1);
        vector<int> vect_t2 = number_to_vector(t2);
        
        if (n == 1)
            return vect_t1;
        if (n == 2)
            return vect_t2;
            
       
        // Compute Fibonacci numbers using the conventional dynamic programming method
        vector<int> vect_t3;
        
        for (int i = 2; i < n ; i++)
        {
            
            
            vect_t3 =  clean_ans(my_sum_raw( vect_t1 , my_square_2( vect_t2 ) ));
           
            vect_t1 = vect_t2;
            vect_t2 = vect_t3;
        }
    
        return vect_t3;
    }
    // At the end it is strange that the answer is too big to be int, but the return type of the  function was int. I changed it to a vector. Therefore I had to change the print function too
    int main()
    {
        ofstream fout(getenv("OUTPUT_PATH"));
    
        string first_multiple_input_temp;
        getline(cin, first_multiple_input_temp);
    
        vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
    
        int t1 = stoi(first_multiple_input[0]);
    
        int t2 = stoi(first_multiple_input[1]);
    
        int n = stoi(first_multiple_input[2]);
    
        vector<int> result = fibonacciModified(t1, t2, n);
    
        for (int i = result.size()-1 ; i >= 0 ; i--)
        {
            fout << result[i];
        }
        fout << "\n";
    
    
        fout.close();
    
        return 0;
    }