GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — coding-problems/Math/prime_factors.py at master · BiKodes/coding-problems Expand file tree/ Copy pathprime_factors.pyMore file actions 62 lines (46 loc) · 1.55 KB / Copy pathprime_factors.pyFile metadata and controls62 lines (46 loc) · 1.55 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 ''' Prime Factors of Integers As the fundamental theorem of arithmetic again reminds us, every positive integer can be broken down into the product of its prime factors exactly one way, disregarding the order of listing these factors. Given positive integer n > 1, return the list of its prime factors in sorted ascending order, each prime factor included in the list as many times as it appears in the prime factorization of n. Input: 42 Output: [2, 3, 7] ========================================= While n is divisible by 2, save all 2 factors and divide n by 2. Now n is odd, so you won't need to check if divisible by some even number, because of that starting from 3 jump by 2 numbers. Time Complexity: O(N) , if prime number then N/2 checks, if all prime factors are 2 then LogN checks Space Complexity: O(LogN) , if all prime factors are 2, else less than LogN space ''' ############ # Solution # ############ def prime_factors(n): factors = [] while n % 2 == 0: factors.append(2) n //= 2 # now n is odd i = 3 while i * i <= n: while n % i == 0: factors.append(i) n //= i # increase by 2, no need to check if divisbile by even numbers i += 2 if n > 2: factors.append(n) return factors ########### # Testing # ########### # Test 1 # Correct result => [2, 3, 7] print(prime_factors(42)) # Test 2 # Correct result => [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5] print(prime_factors(10**6)) # Test 3 # Correct result => [127, 9721] print(prime_factors(1234567)) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |