示例#1
0
文件: 5.py 项目: elemel/project-euler
def solve():
    max_factors = defaultdict(int)
    for i in xrange(1, 21):
        factors = defaultdict(int)
        for j in factor(i):
            factors[j] += 1
        for k in factors:
            max_factors[k] = max(max_factors[k], factors[k])
    return product(k ** v for k, v in max_factors.iteritems())
示例#2
0
文件: sub.py 项目: cwenck/ctf-crypto
def findRepeatedSequenceOffsetsAndCounts(text):
	# Remove all spaces
	text = text.replace(' ', '')
	allSeq = []

	for i in range(0, len(text) - 3):
		allSeq.append([i, text[i:i+3]])
		allSeq.append([i, text[i:i+4]])

	lastStart = len(text) - 3
	allSeq.append([lastStart, text[lastStart:]])

	# Now all possible sequences of 3 and 4 chars are stored in allSeq

	def keyFunc(item):
		return item[1]

	# Sort it based on text contents, so that those that match are back to back
	sortedSeq = sorted(allSeq, key=keyFunc)

	matchDiffs = {}

	lastItem = None
	for item in sortedSeq:
		if lastItem is None:
			lastItem = item
			continue

		if lastItem[1] == item[1]:
			diff = item[0] - lastItem[0]
			if diff in matchDiffs:
				matchDiffs[diff] += 1
			else:
				matchDiffs[diff] = 1

		lastItem = item
	# END FOR

	matchDiffsAndFactors = dict(matchDiffs)
	for key, value in matchDiffs.items():
		# print(str(key) + ':'+ str(value))
		factors = util.factor(key)
		for factor in factors:
			if factor in matchDiffsAndFactors:
				matchDiffsAndFactors[factor] += value
			else:
				matchDiffsAndFactors[factor] = value

	return sorted(matchDiffsAndFactors.items(), key=operator.itemgetter(1), reverse=True)
示例#3
0
文件: e0005.py 项目: eykd/euler
def smallestDivisibleByRange(start, end):
    factor_count = defaultdict(lambda: 1)

    # Analyze the maximum number of times a factor appears.
    for n in xrange(max(start, 2), end+1):
        local_count = defaultdict(lambda: 0)
        factors = factor(n)
        for f in factors:
            local_count[f] += 1

        for f, count in local_count.iteritems():
            factor_count[f] = max(factor_count[f], count)


    n = 1
    for f, count in factor_count.iteritems():
        n *= (f**count)
            
    for d in range(max(start, 2), end+1):
        assert not n % d, (n, d)

    return n
示例#4
0
文件: e0003.py 项目: eykd/euler
def main():
    return max(factor(600851475143))
示例#5
0
"""
This file contains a solution for the fifth Project Euler problem.
https://projecteuler.net/problem=5

Author: Clinton Morrison
File:   005.py
"""
import util

lcm_factors = {}

for a in xrange(1, 21):
    for (factor, count) in util.factor(a).iteritems():
        lcm_factors[factor] = max(count, lcm_factors.get(factor, 0))

result = 1
for (factor, count) in lcm_factors.iteritems():
    result *= factor**count

print result

示例#6
0
#! /usr/bin/python

from util import factor

factors = factor(28124)
for f in range(len(factors)):
    factors[f].remove(f)

abundant = [i for i in range(1, 28124) if i < sum(factors[i])]

result = 0
for i in range(1, 28124):
    print i,
    can_be = False
    for a in abundant:
        if a > (i / 2):
            break
        if i - a in abundant:
            can_be = True
            break
    if not can_be:
        result += i
print result
示例#7
0
文件: p23.py 项目: andermic/euler
#! /usr/bin/python

from util import factor

factors = factor(28124)
for f in range(len(factors)):
    factors[f].remove(f)

abundant = [i for i in range(1,28124) if i < sum(factors[i])]

result = 0
for i in range(1,28124):
    print i,
    can_be = False
    for a in abundant:
        if a > (i/2):
            break
        if i - a in abundant:
            can_be = True
            break
    if not can_be:
        result += i
print result
示例#8
0
"""What is the value of the first triangle number to have over five hundred
divisors?
"""
from operator import mul

from util import factor, triangle_sequence


for n in triangle_sequence():
  old = 1
  m = 0
  div = []
  for f in factor(n):
    if f != old:
      div.append(m)
      m = 1
    else: m += 1
    old = f
  if div and reduce(mul, map(lambda n: n + 1, div)) - 1 > 500:
    print n
    break
示例#9
0
def numfactors_alt(n):
    return 0 if n<1 else 1 if n<2 else reduce(mul,[b+1 for (a,b) in factor(n)])
示例#10
0
文件: 3.py 项目: elemel/project-euler
def solve():
    return max(factor(600851475143))
示例#11
0
"""What is the largest prime factor of the number 600851475143 ?
"""
from util import factor

print[f for f in factor(600851475143)][-1]
示例#12
0
"""
This file contains a solution for the third Project Euler problem.
https://projecteuler.net/problem=3

Author: Clinton Morrison
File:   003.py
"""
import util

print max(util.factor(600851475143).keys())
示例#13
0
def divisorcount(n):
    factorcount = defaultdict(int)
    for i in factor(n):
        factorcount[i] += 1
    return product(count + 1 for count in factorcount.itervalues())