Project Euler #45: Triangular, pentagonal, and hexagonal

  • + 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))