Project Euler Problem#28

#python program to find the sum of diagonals of spiral number grid
import unittest

def spiral_dia(num):
    if type(num)!=int or num<1:
        return None
    list1=[]
    for i in range(1,num+1,2):
        list1.append(i*i)
    for i in range(1,num,2):
        list1.append((i*i)+(i+1))
    for i in range(2,num+1,2):
        list1.append((i*i)+1)
    for i in range(2,num+1,2):
        list1.append((i*i)+(i+1))
    return sum(list1)

print(spiral_dia(1001))

class TestCases(unittest.TestCase):

    def test_spiral_dia(self):
        self.assertEqual(spiral_dia(-5),None)
        self.assertEqual(spiral_dia(5),101)
        self.assertEqual(spiral_dia("5"),None)

if __name__ == "__main__":
    unittest.main()
0
Subscribe to my newsletter

Read articles from Varjinth subramaniyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Varjinth subramaniyan
Varjinth subramaniyan