---
# System prepended metadata

title: 2614. Prime In Diagonal
tags: [Leetcode, Easy, Math]

---

# 2614. Prime In Diagonal
###### tags: `Leetcode` `Easy` `Math`
Link: https://leetcode.com/problems/prime-in-diagonal/description/
## 思路
可以暴力解
也可以用Sieve Algo算出global的isPrime array
## Code
```python=
class Solution:
    maxNum = 4000001
    isPrime = [True] * maxNum
    isPrime[1] = False
    def buildSieve(self):
        for i in range(2, int(math.sqrt(self.maxNum))):
            if self.isPrime[i]:
                for j in range(i*i, self.maxNum, i):
                    self.isPrime[j] = False

    def diagonalPrime(self, nums: List[List[int]]) -> int:
        if self.isPrime[4]:
            self.buildSieve()
        ans = 0
        for i in range(len(nums)):
            if self.isPrime[nums[i][i]]:
                ans = max(ans, nums[i][i])
            if self.isPrime[nums[i][len(nums)-i-1]]:
                ans = max(ans, nums[i][len(nums)-i-1])
        return ans
```