Download src/llm_benchmark/algorithms/primes.py from Prabodini/Test01: direct link, hf CLI and curl.
- Browser
- Download file 1.68 kB
-
https://huggingface.co/spaces/Prabodini/Test01/resolve/main/src/llm_benchmark/algorithms/primes.py
- Command line
-
hf download hf://spaces/Prabodini/Test01/src/llm_benchmark/algorithms/primes.py
-
curl -L -o primes.py https://huggingface.co/spaces/Prabodini/Test01/resolve/main/src/llm_benchmark/algorithms/primes.py
1.68 kB
| from math import isqrt | |
| from typing import List | |
| class Primes: | |
| def is_prime(n: int) -> bool: | |
| """Check if a number is prime | |
| Args: | |
| n (int): Number to check | |
| Returns: | |
| bool: True if the number is prime, False otherwise | |
| """ | |
| if n < 2: | |
| return False | |
| for i in range(2, isqrt(n) + 1): | |
| if n % i == 0: | |
| return False | |
| return True | |
| def is_prime_ineff(n: int) -> bool: | |
| """Check if a number is prime (inefficiently) | |
| Args: | |
| n (int): Number to check | |
| Returns: | |
| bool: True if the number is prime, False otherwise | |
| """ | |
| if n < 2: | |
| return False | |
| for i in range(2, isqrt(n) + 1): | |
| if n % i == 0: | |
| return False | |
| return True | |
| def sum_primes(n: int) -> int: | |
| """Sum of primes from 0 to n (exclusive) | |
| Args: | |
| n (int): Number to sum up to | |
| Returns: | |
| int: Sum of primes from 0 to n | |
| """ | |
| sum_ = 0 | |
| for i in range(n): | |
| if Primes.is_prime(i): | |
| sum_ += i | |
| return sum_ | |
| def prime_factors(n: int) -> List[int]: | |
| """Prime factors of a number | |
| Args: | |
| n (int): Number to factorize | |
| Returns: | |
| List[int]: List of prime factors | |
| """ | |
| ret = [] | |
| while n > 1: | |
| for i in range(2, n + 1): | |
| if n % i == 0: | |
| ret.append(i) | |
| n = n // i | |
| break | |
| return ret | |