def _gcf(first, second): """<first> <second> Finds the greatest common factor of two numbers.""" first = factors(first) second = factors(second) common = set(first) & set(second) return sorted(common)[-1]
def aspect_ratio(number, thumbsize, aspect_ratio): """ Find the best number of rows and columns to approximate a given aspect ratio: number The number of images thumbsize The width and height of each image (eg 80, 100) aspect_ratio The width and height of an aspect ratio (eg 16,9) """ from math import sqrt total_area = number * thumbsize[0] * thumbsize[1] ideal_width = sqrt(total_area * aspect_ratio[0] / aspect_ratio[1]) ideal_height = total_area / ideal_width print(ideal_width, "x", ideal_height, "=", total_area) print(ideal_width, "x", ideal_height, "=", ideal_width * ideal_height) best_overlap = 0 best_factor = None factors_list = factors.factors(number) for factor in factors_list: width = factor * thumbsize[0] height = total_area / width overlap = min(width, ideal_width) * min(height, ideal_height) if overlap > best_overlap: best_overlap = overlap best_factor = factor # print(factor, "\t", overlap, "\t", best_overlap) cols = int(best_factor) rows = int(number / cols) print(cols, "cols,", rows, "rows") return cols, rows
def d(n): if n in cache: return cache[n] f = factors.factors(primes.prime_factors(n,prime_list)) f.remove(n) cache[n] = sum(f) return sum(f)
def test_factors(n): ''' Ensure the factors of n multiply to give n. ''' product = 1 for f in factors(n): product *= f assert product == n
def test_ascending(n): ''' Ensure the factors are in ascending order. ''' prev = None for f in factors(n): if prev: assert f >= prev prev = f
def main(): start = time.time() factores = set() orden = 1 num_triang = 0 while len(factores) < 500: num_triang = orden + num_triang factores = factors(num_triang) orden = orden + 1 elapsed = (time.time() - start) print("%s found in %s seconds" % (num_triang,elapsed))
def divisors(n): fs = list(factors(n)) groups = [] i = 0 while i < len(fs): g = [1, int(fs[i])] j = i + 1 while j < len(fs) and fs[j] == fs[i]: g.append(int(fs[i]) * g[-1]) j += 1 groups.append(str(g)) i = j r = [] for div_fs in eval("itertools.product(" + string.join(groups, ',') + ")"): r.append(prod(div_fs)) return r
def __init__(self, parent): """ Parent is a hook to features of the parent gui """ self.parent = parent # peaks are in self.parent.finalpeaks self.menuitems = ("Factors", 0, [("Load chi files", 0, self.loadchis), ("Save obsdata in binary", 0, self.saveobsdata), ("Read obsdata in binary", 0, self.readobsdata), ("Plot a 1D", 0, self.plotchi), ("Plot obs colormap", 0, self.plotsurf), ("Generate SVD", 0, self.factorsvd), ("Save SVD", 1, self.savesvd), ("Load SVD", 0, self.loadsvd), ("Select number of factors", 0, self.setnfactors), ("Generate differences", 9, self.dataminuscalc)]) self.factors = factors.factors()
def problem003(argument = 600851475143): """Return the maximum factor.""" return max(factors(argument))
import factors abundants = [] sums = [False] * 28123 for n in xrange(1, 28123): if sum(factors.factors(n)) - n > n: abundants.append(n) for i in xrange(0, len(abundants)): for j in xrange(i, len(abundants)): a = abundants[i] b = abundants[j] if a + b >= len(sums): break sums[a + b] = True s = 0 for i, sum in enumerate(sums): if not sum: s += i print s
from factors import factors from time import time number = 0 number = int(input("Enter a number: ")) # Start the timer start = time() result = factors(number) # Start the timer end = time() print(result) print(str(end - start) + " seconds")
import factors n = 1 i = 2 while len(factors.factors(n)) <= 500: n += i i += 1 print n
def test_26(): assert list(factors(26)) == [2, 13]
# -*- coding: utf-8 -*- """ Created on Fri Jun 16 04:20:52 2017 @author: Steven """ import timeit from factors import factors start = timeit.default_timer() def generate_nth_triangle(n): number = 0 for i in range(1,n+1): number += i return(number) max_factors = 1 n = 0 while max_factors < 500: n+=1 i = generate_nth_triangle(n) if len(factors(i)) > max_factors: max_factors = len(factors(i)) print(i, " - ", max_factors) stop = timeit.default_timer() print (stop - start,"seconds")
def test_11(): assert list(factors(11)) == [11]
def test_5(): assert list(factors(5)) == [5]
from factors import factors from time import time minimum = int(input("Enter a minimum number: ")) maximum = int(input("Enter a minimum number: ")) increment = int(input("Enter a number to increment by: ")) for i in range(minimum, maximum, increment): # Start the timer start = time() result = factors(i) # Start the timer end = time() total = end - start print("%s, %s" % (i, total))
def test_0(): assert list(factors(0)) == []
#Let us list the factors of the first seven triangle numbers: #1: 1 #3: 1,3 #6: 1,2,3,6 #10: 1,2,5,10 #15: 1,3,5,15 #21: 1,3,7,21 #28: 1,2,4,7,14,28 #We can see that 28 is the first triangle number to have over five divisors. # #What is the value of the first triangle number to have over five hundred divisors? import primes import factors def triangle_generator(): tmp = 1 while True: yield tmp * (tmp+1) // 2 tmp += 1 prime_list = primes.primes(1000000) for t in triangle_generator(): tmp = factors.factors(primes.prime_factors(t, prime_list)) if len(tmp) > 500: print(t) exit()
def test_prime(n): ''' All the factors should be prime numbers. ''' for f in factors(n): assert is_prime(f)
def test_error(n): ''' Numbers less than or equal to 1 don't have prime factors. ''' with pytest.raises(ValueError): list(factors(n))
def _factorsWrapper(*n): res = factors(entry.get()) res = ", ".join(str(i) for i in res) out.set(res)
def test_100(): assert list(factors(100)) == [2, 2, 5, 5]
def test_4(): assert list(factors(4)) == [2, 2]
def test_121(): assert list(factors(121)) == [11, 11]
def test_13(): assert list(factors(13)) == [13]
def test_property(n): prime_factors = list(factors(n)) product = 1 for i in prime_factors: product *= i assert product == n
def phi(n): fs = set(factors(n)) r = n / prod(fs) r *= prod(x - 1 for x in fs) return r
– Start by setting t equal to p−1,i.e.,t=p1 p2...pn. – For each prime factor pi, do the following: 1. Test if a t′ ≡1(modp) where t′ =t/pi. 2. If this is true, then pi must not be a prime factor of t; so set t to equal t′. 3. If this is false, then pi is a prime factor; so do not change t. """ def order(p,factors,a): t = p-1 for f in factors: tt = t/f print "tt:", tt if(p%(a**tt)==1): t = tt return t ##################################################################### # Tests import Crypto.Util.number as CUN import factors as FAC if __name__ == "__main__": p = 17 #CUN.getPrime(20) f = FAC.factors(p-1) a = 4 print p, f, a print order(p,f,a)
import factors s = 0 for i in xrange(2, 10000): sf = sum(factors.factors(i)) - i if sf != i and sf < 10000 and sum(factors.factors(sf)) - sf == i: s += i + sf print s / 2
def totient(n, primes): n_factors=factors(n, primes) for i in range(1, n):
def d(n): return sum(factors(n)[:-1])
def isPrime(n): """Returns whether the given number is prime.""" return n > 1 and len(factors(n)) == 2
def is_abundant(n): f = factors(n)[:-1] return sum(f) > n
def _factorsWrapper(*n): res = factors(entry.get()) res = ', '.join(str(i) for i in res) out.set(res)
def test_36(): assert list(factors(36)) == [2, 2, 3, 3]
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: 1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? """ from factors import factors triangle_number = 1 next_natural = 2 while len(factors(triangle_number)) < 500: triangle_number += next_natural next_natural += 1 print(triangle_number)
1. Test if a t′ ≡1(modp) where t′ =t/pi. 2. If this is true, then pi must not be a prime factor of t; so set t to equal t′. 3. If this is false, then pi is a prime factor; so do not change t. """ def order(p, factors, a): t = p - 1 for f in factors: tt = t / f print "tt:", tt if (p % (a**tt) == 1): t = tt return t ##################################################################### # Tests import Crypto.Util.number as CUN import factors as FAC if __name__ == "__main__": p = 17 #CUN.getPrime(20) f = FAC.factors(p - 1) a = 4 print p, f, a print order(p, f, a)