Project Euler #45: Triangular, pentagonal, and hexagonal

Sort by

recency

|

29 Discussions

|

  • + 0 comments

    python 100%

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    N, n1, n2 =  map(int, input().split())
    
    # dict acting like a dispatcher to calculate series value
    l_n = { 3 : lambda x: (x*(x+1))//2 , 5: lambda x: (x*(3*x-1))//2 , 
                        6: lambda x: x*(2*x - 1)  }
        
    nc1, nc2 = 1, 1 # two counters to catch up the series without saving value
    
    while True:
        val_n2, val_n1 = l_n[n2](nc2), l_n[n1](nc1)
        ''' compute for the higher order series and let the lower value series catch up '''
        while val_n1 < val_n2:
            nc1 += 1
            val_n1 = l_n[n1](nc1)
        if val_n1 >= N:
            break
        if val_n1 == val_n2:
            print(val_n1)
        nc2 += 1
        
    
  • + 0 comments

    Simple and understandable solution in Python :

    def is_triangular(x):
        return True if (1+8*x)**0.5==int((1+8*x)**0.5) else False
    def is_pentagonal(x):
        return True if (1+24*x)**0.5==int((1+24*x)**0.5) and int((1+24*x)**0.5+1)%6==0 else False
        
    n,a,b=map(int,input().strip().split())
    np=int((1+24*n)**0.5+1)//6
    nh=int((1+8*n)**0.5+1)//4
    if a==3:
        for i in range(1,np):
            p=i*(3*i-1)//2
            if is_triangular(p):
                print(p)
    else:
        for i in range(1,nh):
            h=i*(2*i-1)
            if is_pentagonal(h):
                print(h)
    
  • + 0 comments

    Python3. Simple brute force. Boring.

    param = {3:(1, 1, 1), 5:(1, 1, 3), 6:(1, 1, 4)}
    
    def double_seq(n, a, b):
        v1, d1, dd1 = param[a]
        v2, d2, dd2 = param[b]
        while v2 < n:
            if v1 == v2: print(v1)
            if v1 < v2:
                d1 += dd1
                v1 += d1
            else:
                d2 += dd2
                v2 += d2
            
    n, a, b = map(int, input().split())
    double_seq(n, a, b) 
    
  • + 0 comments

    JAva code

    import java.io.*;
    import java.util.*;
    
    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 in = new Scanner(System.in);
            long n = in.nextLong();
            int a = in.nextInt();
            int b = in.nextInt();
            int increment = 4;
    
            for(long i=1;i<n;i++)
            {
                if(a==3)
                {
                    if(isTriangularNumber(i))
                        System.out.println(i);
                    if(i==1) i+=3;
                    else if(i>4)
                    {
                        i+=increment+2;
                        increment+=3;
                    }
                }
                else
                {
                    if(isHexagonalNumber(i))
                        System.out.println(i);
                    if(i==1) i+=3;
                    else if(i>4)
                    {
                        i+=increment+2;
                        increment+=3;
                    }
                }
            }
        }
        
        public static boolean isTriangularNumber(long n)
        {
            double tTest = Math.sqrt(1 + 8*n);
            return tTest == ((long)tTest);
        }
        
        private static boolean isHexagonalNumber(long number)
        {
            double hexTest = (Math.sqrt(1 + 8*number) + 1.0) / 4.0;
            return hexTest == ((long)hexTest);
        }
    }
    
  • + 0 comments

    100/- points python3, mathematical solve

    def triangular(n):
        return (n)*(n+1)//2
    
    def pentagonal(n):
        return n*(3*n-1)//2
    
    def hexagonal(n):
        return n*(2*n-1)
    
    def lint(n):
        if int(n)==n:
            return int(n)
        else:
            return int(n)+1
    triangulars=set()
    pentagonals=set()
    hexagonals=set()
    
    item=input().split()
    
    n=int(item[0])
    a=int(item[1])
    
    i=1
    if a==3:
        for i in range(1,lint((-1+(8*n)**0.5)/2)):
            y=(1+12*(i)*(i+1))**0.5
            if int(y)==y:
                if (1+int(y)) % 6 == 0:
                    print(triangular(i))
       
    else:
        for i in range(1,lint((1+(24*n)**0.5)/6)):
            y=(4+16*i*(3*i-1))**0.5
            if int(y)==y:
                if (2+int(y)) % 8 == 0:
                    print(pentagonal(i))