Esempio n. 1
0
def UNDEEDED_simplify_fraction(numerator, denominator):
    num_factors = util.factors(numerator)
    denom_factors = util.factors(denominator)

    common_factors = set.intersection(num_factors, denom_factors)
    common_factors.remove(1)
    if common_factors:
        divisor = max(common_factors)
        # print(f"found common factor {divisor}")
        numerator, denominator = UNDEEDED_simplify_fraction(
            numerator / divisor, denominator / divisor)

    # print(f"done! returned {numerator} / {denominator}")
    return numerator, denominator
Esempio n. 2
0
def is_perfect(num):
    return sum(factors(num) - {num}) == num
Esempio n. 3
0
def ex7(num):
    print(len(factors(num)) == 2)
Esempio n. 4
0
def ex6(num):
    print(factors(num))
Esempio n. 5
0
def ex5():
    print(factors(120))
Esempio n. 6
0
def factorSum(x):
  return (sum(util.factors(x)) - x)
"""
Project Euler 12: Find the value of the first triangle number to have over five hundred divisors
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 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:
"""

import util

n = 1

while len(util.factors(util.trianglar(n))) <= 500:
	n += 1
print n, util.trianglar(n)
Esempio n. 8
0
import util as ut
import math
"""
https://projecteuler.net/problem=627

Counting products
Consider the set S of all possible products of n positive integers not exceeding m, that is
S={x1x2…xn|1≤x1,x2,...,xn≤m}.
Let F(m,n) be the number of the distinct elements of the set S.
For example, F(9,2)=36 and F(30,2)=308.

Find F(30,10001) mod 1000000007.

"""

factors = ut.factors(9)


def fun_F(m, n):
    # 1*1
    factors = ut.factors(m)
    pass


if __name__ == '__main__':
    modulus = 1_000_000_007
Esempio n. 9
0
def fun_F(m, n):
    # 1*1
    factors = ut.factors(m)
    pass
Esempio n. 10
0
def n_prime_factors(n, num_distinct_factors):
    def _check_prime_factors_multiply(num,
                                      prime_facts,
                                      new_prime_facts=None,
                                      prime_idx_to_exponent=0,
                                      exponent=2):
        product = ut.product(
            new_prime_facts) if new_prime_facts else ut.product(prime_factors)
        if product == num:
            return True

        # if smaller, for each of the prime factors, try increasingly higher powers UNTIL exceed
        elif product < num:
            if new_prime_facts:
                new_prime_facts = new_prime_facts[0:prime_idx_to_exponent] + [
                    new_prime_facts[prime_idx_to_exponent]**exponent,
                ] + new_prime_facts[prime_idx_to_exponent + 1:]

            else:
                new_prime_facts = prime_facts[0:prime_idx_to_exponent] + [
                    prime_facts[prime_idx_to_exponent]**exponent,
                ] + prime_facts[prime_idx_to_exponent + 1:]

            return _check_prime_factors_multiply(num, prime_facts,
                                                 new_prime_facts,
                                                 prime_idx_to_exponent,
                                                 exponent + 1)

        elif product > num and prime_idx_to_exponent + 1 < len(prime_facts):
            # exceeded target number BUT still other
            return _check_prime_factors_multiply(
                num,
                prime_facts,
                new_prime_facts=None,
                prime_idx_to_exponent=prime_idx_to_exponent + 1,
                exponent=2)

        else:
            return False

    consecutive_ints = []
    while len(consecutive_ints) < num_distinct_factors:
        factors = ut.factors(n)

        # filter for prime factors and factors != n
        prime_factors = [
            fact for fact in factors if ut.is_prime(fact)
            if fact not in {1, n}
        ]
        if len(prime_factors) == num_distinct_factors:
            check_outcome = _check_prime_factors_multiply(n, prime_factors)
            if check_outcome:
                consecutive_ints.append(n)
            else:
                consecutive_ints = []

        elif len(prime_factors) != num_distinct_factors and consecutive_ints:
            consecutive_ints = []

        n += 1
    return consecutive_ints
Esempio n. 11
0
def _input_func(s):
    rets = []
    for t in range(1, s, 2):
        if factors(s) & factors(t) == {1}:
            rets.append((a(s, t), b(s, t), c(s, t)))
    return rets
Esempio n. 12
0
"""
The first two consecutive numbers to have two distinct prime factors are:

14 = 2 × 7
15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

644 = 2² × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19.

Find the first four consecutive integers to have four distinct prime factors each. 
What is the first of these numbers?
"""
factors = ut.factors(644)
prime_factors = [fact for fact in factors if ut.is_prime(fact)]


def n_prime_factors(n, num_distinct_factors):
    def _check_prime_factors_multiply(num,
                                      prime_facts,
                                      new_prime_facts=None,
                                      prime_idx_to_exponent=0,
                                      exponent=2):
        product = ut.product(
            new_prime_facts) if new_prime_facts else ut.product(prime_factors)
        if product == num:
            return True

        # if smaller, for each of the prime factors, try increasingly higher powers UNTIL exceed
Esempio n. 13
0
#!/usr/bin/python

import os
import sys
import argparse

#Auto-import parent module
sys.path.insert(1, os.path.join(sys.path[0], '..'))
import util
#from pydmv import util

parser = argparse.ArgumentParser(description="Show all factors of an integer")
parser.add_argument("value", help="An integer")
args = parser.parse_args()
value = int(args.value)

print("Factors of {} are {}".format(value, util.factors(value)))
Esempio n. 14
0
def debug(x):
  print(x, util.factors(x), factorSum(x), isAbundant(x))
Esempio n. 15
0
import numpy as np

from util import factors

""" Find sum of numbers not expressible as the sum of two abundant numbers """

TOP = 28124
abundants = []
nosum = np.ones(TOP, dtype='bool')

# find the abundant numbers below TOP
for i in range(12, TOP):
    if sum(factors(i)[:-1]) > i:
        abundants.append(i)
abundants = np.array(abundants)

# compute each pairwise sum of abundants
# mark the corresponding number as disqualified
for i in range(len(abundants)):
    if abundants[i] > TOP / 2: 
        break
    s = abundants[i] + abundants[i:]
    hit = np.where(s < TOP)
    nosum[s[hit]] = False

#sum up the numbers
ind, = np.where(nosum)
print ind.sum()
Esempio n. 16
0
from util import factors

print max(factors(600851475143))
Esempio n. 17
0
def _input_func(s):
    rets = []
    for t in range(1, s, 2):
        if factors(s) & factors(t) == {1}:
            rets.append((a(s, t), b(s, t), c(s, t)))
    return rets
Esempio n. 18
0
#!/usr/bin/env python
import util
print(max(util.factors(600851475143)))
Esempio n. 19
0
def first_triagle_with_divisors(n):
    triangle_number = 1; i = 1 
    while len(util.factors(triangle_number)) <= n:
        i += 1
        triangle_number += i
    return triangle_number
Esempio n. 20
0
import numpy as np

from util import factors
""" Find sum of numbers not expressible as the sum of two abundant numbers """

TOP = 28124
abundants = []
nosum = np.ones(TOP, dtype='bool')

# find the abundant numbers below TOP
for i in range(12, TOP):
    if sum(factors(i)[:-1]) > i:
        abundants.append(i)
abundants = np.array(abundants)

# compute each pairwise sum of abundants
# mark the corresponding number as disqualified
for i in range(len(abundants)):
    if abundants[i] > TOP / 2:
        break
    s = abundants[i] + abundants[i:]
    hit = np.where(s < TOP)
    nosum[s[hit]] = False

#sum up the numbers
ind, = np.where(nosum)
print ind.sum()
Esempio n. 21
0
from util import factors

top = 10000
d = [0] * (top+1)
for i in range(top):
    d[i] = sum(factors(i)[:-1])

result = 0
for i in range(1, top):
    if d[i] > i and d[i] < top and d[d[i]] == i:
        result += d[i] 
        result += i

print result
Esempio n. 22
0
def answer(n):
    return max(factors(n))