Project Euler #57: Square root convergents

Sort by

recency

|

17 Discussions

|

  • + 0 comments

    100 points. Python 3

    n=int(input().strip())
    numerators=[1]
    denominators=[2]
    i=2
    while i<=n:
        numerators.append(denominators[-1])
        denominators.append(denominators[-1]*2+numerators[-2])
        i+=1
    for i in range(n):
        n=denominators[i]+numerators[i]
        d=denominators[i]
        if len(str(n))>len(str(d)):
            print(i+1)
    
  • + 0 comments

    JAva Code

    import java.io.*;
    import java.util.*;
    import java.math.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner scanner = new Scanner(System.in);
            int N = scanner.nextInt();
            scanner.close();
    
            List<Integer> iterations = new ArrayList<>();
            BigInteger numerator = BigInteger.valueOf(3);
            BigInteger denominator = BigInteger.valueOf(2);
    
            for (int i = 2; i <= N; i++) {
                BigInteger newNumerator = numerator.add(denominator.multiply(BigInteger.valueOf(2)));
                BigInteger newDenominator = numerator.add(denominator);
    
                if (newNumerator.toString().length() > newDenominator.toString().length()) {
                    iterations.add(i);
                }
    
                numerator = newNumerator;
                denominator = newDenominator;
            }
    
            for (int iteration : iterations) {
                System.out.println(iteration);
            }
        }
    }
    
  • + 0 comments

    Paythan

    [def continued_fraction_expansion_sqrt2(N): numerators = [3] denominators = [2] iterations = []

    for i in range(1, N):
        numerator = numerators[-1] + 2 * denominators[-1]
        denominator = numerators[-1] + denominators[-1]
        numerators.append(numerator)
        denominators.append(denominator)
    
        # Check if the numerator has more digits than the denominator
        if len(str(numerator)) > len(str(denominator)):
            iterations.append(i + 1)  # Add 1 to make it 1-based index
    
    return iterations
    

    Input

    N = int(input())

    Get the iterations where the numerator has more digits than the denominator

    result = continued_fraction_expansion_sqrt2(N)

    Output the result

    for iteration in result: print(iteration)](https://)

  • + 0 comments

    include

    using namespace std;

    deque Add(deque a, deque b) { deque res;

    if(a.size() < b.size()) swap(a, b);
    
    short carry = 0;
    
    while(!a.empty())
    {
        short sum = a.back() + ((!b.empty()) ? b.back() : 0) + carry;
    
        carry = sum / 10;
        res.push_front(sum % 10);
        a.pop_back();
    
        if(!b.empty()) b.pop_back();
    }
    while(carry) 
    {
        res.push_front(carry);
        carry /= 10;
    }
    return res;
    

    }

    bool DB = false;

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

    deque<short> N = {1}, D = {2};
    
    for(int i=1; i<=n; i++)
    {        
        if(Add(N, D).size() > D.size())
        {
            cout << i << endl;
        }
    
        deque<short> next_n = Add(D, D);        
        deque<short> next_d = D;
    
        next_n = Add(next_n, N);
    
        N = next_n;
        D = next_d;        
        swap(N, D);
    }        
    return 0;
    

    }

  • + 0 comments

    HINTS:

    This is like an IQ test: Find similarities of series (3/2, 7/5, 17/2, 41/29,...)

    For example with 7/5:

    Numerator (N) 7 = 3 + 2 x 2

    Denominator (D) 5 = 3 + 2

    Then we got:

    N[i] = N[i-1] + 2*D[i-1]

    D[i] = N[i-1] + D[i-1]

    Then you can solve yourself