1. Hey guys,
    Hope you all are doing great. I am back with another problem of Project Euler on

    Project Euler #8: Largest product in a series
    Find the greatest product of K consecutive digits in the N digit number.
    Input Format
    First line contains T that denotes the number of test cases.
    First line of each test case will contain two integers N & K.
    Second line of each test case will contain a N digit integer. 
    Output Format
    Print the required answer for each test case.
    Constraints
    1T100
    1K7
    KN1000
    Sample Input
    2
    10 5
    3675356291
    10 5
    2709360626
    
    Sample Output
    3150
    0
    Algorithm:
    As easy can it get, this is a simple problem to directly multiply the sequence of number.
    Just we need to take care of the thing as the numbers can be very large, so handling them as character sequence is preferable.

    Now its the trick how you convert it into a code. As we already know in Python we have a reduce function
    Ex:
    reduce(sum, range(5))
    Gives an output as 10, as sum is a operator(predefined function in python) which sums up the numbers in range 0 to 4

    Similarly we can use mul from operators in python to reduce a subsequnce of k digits.

    In my below code i have used a simple concept of product, but you can try the above approach to solve it as it worked for me.

    And Presto!! Pythoned!!

    Program:

    from operator import mul
    t=input()
    while(t>0):
        n,m = map(int,raw_input().split())
        num=list(raw_input())
        ans=0
        for i in range(0,n-m):
            x=1
            for k in range(0,m):
                x=x*int(num[i+k])
            ans=max(ans,x)
        print ans
    
    0

    Add a comment

  2. Hey guys,
    Hope you all are doing great. I am back with another problem of Project Euler

    Project Euler #2: Even Fibonacci numbers


    Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
    1,2,3,5,8,13,21,34,55,89,

    By considering the terms in the Fibonacci sequence whose values do not exceed N, find the sum of the even-valued terms.
    Input Format
    First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
    Output Format
    Print the required answer for each test case.
    Constraints
    1T105
    10N4×1016
    Sample Input
    2
    10
    100
    
    Sample Output
    10
    44
    Algorithm:
    Fibonacci Series is one of the most fundamental series in programming world.

    To proceed, you just need to add 2 numbers just prior to your current number. Starting with 1 and 1 it becomes:

    1. 1
    2. 1
    3. 1+1 = 2
    4. 1+2 = 3
    5. 2+3 = 5
    6. and so on....
    Here python comes to our rescue to code it efficiently. As we need only even numbers, will have a condition as num%2 == 2

    Now for the main part, taking a and b as the cursors to our program,
    Initialize them with a=1, b=1
    Then as in Python you can assign INLINE a number of variables so our mail condition becomes
    a , b = a , a+b
    In this way we are handling both previous numbers and calculating the nest number concurrently.


    And Presto!! Its Done!!

    Just we need to convert that into a code, which is easy ion Python, Check the below code:
     Program:


    t=input();
    while (t>0):
        t-=1
        n=input()
        a, b = 1, 1;
        total = 0;
        while a <= n:
            if a % 2 == 0:
                total += a
            a, b = b, a+b  # the real formula for Fibonacci sequence
        print total;
    
    2

    View comments

Loading