Skip to content

Commit

Permalink
[ADD] Python 0012, CPP updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanrosspowell committed Jan 26, 2012
1 parent 7c87cb1 commit f14909c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
5 changes: 2 additions & 3 deletions source/cpp/0012/0012.cpp
Expand Up @@ -73,15 +73,14 @@ int main()
factors = 1; factors = 1;
triangleNumber += counter++; triangleNumber += counter++;
std::vector< int > node; std::vector< int > node;
bool work = true;
int triCopy = triangleNumber; int triCopy = triangleNumber;
while( triCopy != 1 && work ) while( triCopy != 1 )
{ {
for ( int i = 0, prime = 0; prime = primes.getPrime( i ); ++i ) for ( int i = 0, prime = 0; prime = primes.getPrime( i ); ++i )
{ {
if ( triCopy % prime == 0 ) if ( triCopy % prime == 0 )
{ {
triCopy = triCopy / prime; triCopy /= prime;
node.push_back( prime ); node.push_back( prime );
break; break;
} }
Expand Down
37 changes: 18 additions & 19 deletions source/python/0012/0012.py
Expand Up @@ -43,26 +43,25 @@ def getPrime( self, i ):
counter += 1 counter += 1
factorList = [] factorList = []
primeFactors = [] primeFactors = []
primeCount = 0 triCopy = triangleNumber
while True: while triCopy is not 1:
prime = primes.getPrime( primeCount ) primeCount = 0
primeCount += 1
if prime > triangleNumber:
break;
elif triangleNumber % prime == 0:
primeFactors.append( prime )
for factor in primeFactors:
multiple = 1
while True: while True:
factor = prime * multiple prime = primes.getPrime( primeCount )
multiple += 1 primeCount += 1
if factor <= triangleNumber: if triCopy % prime == 0:
factorList.append( factor ) triCopy /= prime
else: primeFactors.append( prime )
break break
num = len( set( factorList ) ) counts = {}
if num >= 500: for factor in primeFactors:
if counts.has_key( factor ):
counts[ factor ] += 1
else:
counts[ factor ] = 1
num = 1
for key, value in counts.items():
num *= ( value + 1 )
if num > 500:
break break
else:
print "Fail %d factors %s" % ( triangleNumber, num )
print triangleNumber print triangleNumber

0 comments on commit f14909c

Please sign in to comment.