Project Euler #28: Number spiral diagonals

  • + 0 comments

    100/- points python3.
    easy solve

    def sum_calc(n):
        if n==1:
            return 1
        sum=1
    #1,3,5,7,9,13,17,21,25,31
    #for i in range(3,n+1,2):
        #sum+=(i**2)*4-6*(i-1)
    # it would be slower to do the above so I just compute it mathematically as a formula by the help of sum of first n squares
        x=(n-1)//2
        sum+=(n*(n+1)*(2*n+1)//6-1-4*(x*(x+1)*(2*x+1)//6))*4-6*(x)*(x+1)
        return sum
    for _ in range(int(input())):
        n=int(input())
        print(sum_calc(n)%(10**9+7))