Project Euler #28: Number spiral diagonals

  • + 0 comments
    def calculate_result(n):
        # Calculate the result using the given formula
        w = n - 1
        result = 1 + 4 * ((n * (2 * n + 1) * (2 * n - 1)) // 3 - 1) - 12 * ((w * (w + 1)) // 2)
        return result % (10**9 + 7)
    
    def main():
        t = int(input(""))
    
        for _ in range(t):
            n = int(input()) // 2 + 1
            result = calculate_result(n)
            print(result)
    
    if __name__ == "__main__":
        main()