This commit is contained in:
bt3gl 2023-07-30 21:40:09 -07:00
parent 1d44d182e2
commit a85ed914d3
320 changed files with 0 additions and 0 deletions

37
math/README.md Normal file
View file

@ -0,0 +1,37 @@
## math, logic, dynamic programming
<br>
### dynamic programming
<br>
* take a recursive algorithm and cache overlapping problems (repeated calls).
* the runtime is given by the number of calls.
* **top-down**: how can we divide the problem into sub-problems?
* top-down dynamic programming is called **memoization**.
* **bottom-up**: solve for a simple case, then figure out for more elements.
<br>
---
### examples
<br>
```python
python3 fibonacci.py
Testing fibonacci
Fibonacci of 10: 55
```
<br>
```python
python playing_with_math.py
Greatest common divider of 21 and 7 is 7
Prime factors of 21 are [3, 7]
```

View file

@ -0,0 +1,53 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
```
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits 1-9 without repetition.
- Each column must contain the digits 1-9 without repetition.
- Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
Input: board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true
```
def is_valid_sudoku(board) -> bool:
N = 9
rows = [set() for _ in range(N)]
cols = [set() for _ in range(N)]
boxes = [set() for _ in range(N)]
for r in range(N):
for c in range(N):
val = board[r][c]
if val == '.':
continue
if val in rows[r]:
return False
rows[r].add(val)
if val in cols[c]:
return False
cols[c].add(val)
index = (r // 3) * 3 + c // 3
if val in boxes[index]:
return False
boxes[index].add(val)
return True

16
math/fibonacci.py Normal file
View file

@ -0,0 +1,16 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def fibonacci(n):
""" Calculate the nth Fibonacci number """
if n == 0 or n == 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == '__main__':
print('Testing fibonacci')
n = 10
print(f'Fibonacci of {n}: {fibonacci(n)}')

24
math/happy_number.py Normal file
View file

@ -0,0 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def get_next(n):
total_sum = 0
while n > 0:
n, digit = divmod(n, 10)
total_sum += digit**2
return total_sum
def is_happy(self, n: int) -> bool:
seen = set()
while n != 1 and n not in seen:
seen.add(n)
n = get_next(n)
return n == 1

51
math/playing_with_math.py Normal file
View file

@ -0,0 +1,51 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
import math
import random
def find_greatest_common_divider(a, b) -> int:
'''Implements the greatest common divider algorithm '''
while(b != 0):
result = b
a, b = b, a % b
return result
def _is_prime(number) -> bool:
'''Check if a number is prime '''
if number < 2:
return False
for i in range(2, int(math.sqrt(number))):
if number % i == 0:
return False
return True
def find_prime_factors(number) -> list:
'''Find prime factors of a number '''
divisors = [d for d in range(2, number//2 + 1) if number % d == 0]
primes = [d for d in divisors if _is_prime(d)]
return primes
if __name__ == '__main__':
n1 = 21
n2 = 7
print(f'Greatest common divider of {n1} and {n2} is {find_greatest_common_divider(n1, n2)}')
print(f'Prime factors of {n1} are {find_prime_factors(n1)}')