Permalink
Browse files

[ADD] Python 0012, CPP updates

  • Loading branch information...
1 parent 7c87cb1 commit f14909c0fd0614856b26a72f874b1f144ff59cd9 @nathanrosspowell committed Jan 26, 2012
Showing with 20 additions and 22 deletions.
  1. +2 −3 source/cpp/0012/0012.cpp
  2. +18 −19 source/python/0012/0012.py
View
@@ -73,15 +73,14 @@ int main()
factors = 1;
triangleNumber += counter++;
std::vector< int > node;
- bool work = true;
int triCopy = triangleNumber;
- while( triCopy != 1 && work )
+ while( triCopy != 1 )
{
for ( int i = 0, prime = 0; prime = primes.getPrime( i ); ++i )
{
if ( triCopy % prime == 0 )
{
- triCopy = triCopy / prime;
+ triCopy /= prime;
node.push_back( prime );
break;
}
View
@@ -43,26 +43,25 @@ def getPrime( self, i ):
counter += 1
factorList = []
primeFactors = []
- primeCount = 0
- while True:
- prime = primes.getPrime( primeCount )
- primeCount += 1
- if prime > triangleNumber:
- break;
- elif triangleNumber % prime == 0:
- primeFactors.append( prime )
- for factor in primeFactors:
- multiple = 1
+ triCopy = triangleNumber
+ while triCopy is not 1:
+ primeCount = 0
while True:
- factor = prime * multiple
- multiple += 1
- if factor <= triangleNumber:
- factorList.append( factor )
- else:
+ prime = primes.getPrime( primeCount )
+ primeCount += 1
+ if triCopy % prime == 0:
+ triCopy /= prime
+ primeFactors.append( prime )
break
- num = len( set( factorList ) )
- if num >= 500:
+ counts = {}
+ 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
- else:
- print "Fail %d factors %s" % ( triangleNumber, num )
print triangleNumber

0 comments on commit f14909c

Please sign in to comment.