示例#1
0
def findTriangularNumberWithNDivisors(num_divisors):
    """
  Find the first triangular with at least num_divisors divisors
  """
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    number_found = False
    triangular_num = 1
    next_num = 1
    list_of_primes = primes.primeSeive(1e6)
    while not number_found:
        next_num += 1
        triangular_num += next_num

        # print triangular_num
        prime_factors = getPrimeFactors(triangular_num, list_of_primes)
        num_factors = getNumDivisorsFromPrimes(prime_factors)
        divisors = getDivisorsFromPrimes(prime_factors)
        # divisors = getDivisors(triangular_num)
        if next_num % 1000 == 0:
            print "%d - %d" % (triangular_num, num_factors)
        if num_factors >= num_divisors:
            print "Found %d with %d divisors" % (triangular_num, num_factors)
            number_found = True

    print "---------------------------------------------------------------------"
    print "The first triangular number with at least %d divisors is %d " % (num_divisors, triangular_num)
示例#2
0
def findAmicableNumbersBelowValue(end):
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    # list_of_primes = primes.primeSeive(end**2)
    list_of_primes = primes.primeSeive(end * 1000)
    num_checked = [False] * (end + 1)
    is_amicable_num = [False] * (end + 1)

    for i in xrange(end + 1):
        if num_checked[i] == True:
            continue

        num_checked[i] = True
        sum_of_divisors = sumOfDivisors(i, list_of_primes)
        print "i: %d - d(%d): %d - d(%d): %d" % (
            i,
            i,
            sum_of_divisors,
            sum_of_divisors,
            sumOfDivisors(sum_of_divisors, list_of_primes),
        )
        if not sum_of_divisors == i and sumOfDivisors(sum_of_divisors, list_of_primes) == i:
            # print '\tamicable pair found'
            is_amicable_num[i] = True
            if sum_of_divisors < end + 1:
                is_amicable_num[sum_of_divisors] = True
                num_checked[sum_of_divisors] = True

    amicable_nums = []
    for i in xrange(end + 1):
        if is_amicable_num[i]:
            amicable_nums.append(i)
    return amicable_nums
示例#3
0
def findCircularPrimesBelow(terminal):
  """docstring for findCircularPrimesBelow"""
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  prime_seive = primes.primeSeive(terminal)
  # print prime_seive
  checked = [False]*terminal
  circular_primes = []
  num_checked = 0
  for i in xrange(terminal):
    if checked[i]:
      continue
    
    if num_checked%1000 == 0:
      print 'checking %dth digit -- %d' % (num_checked, i)
    num_checked+=1

    # circle_prime = (not i%10 == 0)
    # circle_prime = True
    circle_prime = firstPass(i)
    last_num = i
    next_num = -1
    while not next_num == i and circle_prime:
      next_num = rotateNum(last_num)
      # if not len(str(next_num)) == len(str(i)):
      if next_num < i:
        circle_prime = False
        break
      circle_prime = circle_prime and next_num in prime_seive
      last_num = next_num
      checked[next_num] = True
    # print 'i: %d - is circular prime: %s' % (i, circle_prime)
    
    if circle_prime: 
      # print 'saving circular primes!'
      last_num = i
      next_num = -1
      while not next_num == i:
        next_num = rotateNum(last_num)
        # print '\tnext_num: %d' % next_num
        circular_primes.append(next_num)
        last_num = next_num
  
  # circular_primes = []
  # for i, p in enumerate(is_circular_prime):
  #   if p: circular_primes.append(i)
  print 'There are %d circular primes less than %d' % (len(circular_primes), terminal)

  return circular_primes
示例#4
0
def getListOfAbundantNumbers(min = -1, max = -1):
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  if min < 0: min = 0
  # this number is chosen based on the problem statement
  if max < 0: max = 28125

  list_of_primes = primes.primeSeive(max)
  abundant_nums = []

  for i in xrange(min, max+1):
    # if abundant number
    if sumOfDivisors(i, list_of_primes) > i:
      # print '------------------------------------'
      # print '%d is an abundant number' % i
      # print '\tprime divisors: %s' % primes.getPrimeFactors(i, list_of_primes)
      # print '\tsum of divisors: %d' % sumOfDivisors(i, list_of_primes)
      abundant_nums.append(i)

  return abundant_nums
示例#5
0
def findNumsWithDumbReduction():
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  reducible_num = []
  reducible_den = []

  for den in xrange(11, 100):
    if den%10 == 0:
      continue
    for num in xrange(11, den):
      if num%10 == 0:
        continue
      reduced = doDumbReduction(num, den)
      red_frac = float(reduced['num'])/reduced['den']
      if float(num)/den == red_frac:
        reducible_num.append(reduced['num'])
        reducible_den.append(reduced['den'])

  print 'reduced nums: %s' % reducible_num
  print 'reduced denoms: %s' % reducible_den
  red_product_num = reduce(operator.mul, reducible_num)
  red_product_den = reduce(operator.mul, reducible_den)
  print 'product of reduced nums: %d'   % red_product_num
  print 'product of reduced denoms: %d' % red_product_den
  print 'product of reduced fracs: %f'  % (float(red_product_num)/red_product_den)

  primes_list = primes.primeSeive(red_product_num)
  print primes_list
  prime_factors_num = collections.Counter( primes.getPrimeFactors( red_product_num
                                                                 , primes_list)
                                                                 )
  prime_factors_den = collections.Counter( primes.getPrimeFactors( red_product_den
                                                                 , primes_list)
                                                                 )
  common_prime_factors = list((prime_factors_num & prime_factors_den).elements())
  for p in common_prime_factors:
    red_product_num /= p
    red_product_den /= p
  print 'Fully reduced product of special fractions is %d/%d = %f' % ( red_product_num
                                                                     , red_product_den
                                                                     , ( float(red_product_num)/red_product_den)
                                                                     )
示例#6
0
def getSumOfPrimesLessThan(max_value):
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  if isinstance(max_value, float): max_value = int(max_value)
  # list_of_primes = primes.getPrimesBelowN(max_value)
  list_of_primes = primes.primeSeive(max_value)
  return sum(list_of_primes)