Facebook
From Sohrab Yavarzadeh, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 164
  1. def not_prime(num):
  2.  
  3.     if num > 1:
  4.        # check for factors
  5.        for i in range(2,num):
  6.            if (num % i) == 0:
  7.                return True
  8.                print(i,"times",num//i,"is",num)
  9.                break
  10.        else:
  11.            return False
  12.            
  13.     # if input number is less than
  14.     # or equal to 1, it is not prime
  15.     else:
  16.        return True
  17.  
  18.  
  19.  
  20.  
  21.  
  22. def maxPathSum(tree, m, n):
  23.  
  24.     # loop for bottom-up calculation
  25.     for i in range(m-1, -1, -1):
  26.         for j in range(i+1):
  27.  
  28.            
  29.             if (tri[i+1][j] > tri[i+1][j+1] and not_prime(tri[i+1][j])):
  30.                 tri[i][j] += tri[i+1][j]
  31.             elif not_prime(tri[i+1][j+1]):
  32.                 tri[i][j] += tri[i+1][j+1]
  33.  
  34.    
  35.     return tri[0][0]
  36.  
  37.  
  38. tri = [[1,0,0,0],
  39.        [8,4,0,0],
  40.        [2,6,9,0],
  41.        [8,5,9,3]]
  42. print maxPathSum(tree, 3, 3)

Replies to developer rss

Title Name Language When
developer Sohrab Yavarzadeh python 3 Years ago.