Open in TheBrain

Euler3 Largest Prime Factor

<p >The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ? &#160;Problem 3

&#160;

Feb 13, 2013 08:47 PM

&#160;

key idea is to divide it down as you test each prime!

&#160;

intermediate steps...

&#160;

primes = [2,3,5,7,11,13,17,19,23]

&#160;

this works &#160;(see list comprehensions in for loops... note: &#160;NO fking &#160;colon!)

&#160;

def primes():

&#160;&#160;&#160;&#160;&#160;&#160;numbers = [i for i in range (25) if i in primes]

&#160;&#160;&#160;&#160;&#160;&#160;print numbers

&#160;

#note: NO colon above &#160;in the for loop!!!

&#160;

below works:

#cand is a candidate

def nonprim(cand):

&#160;&#160;&#160;&#160;&#160;&#160;for p in primes:

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if cand%p ==0:

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;print cand,"=",p,"*",int(cand/p)

&#160;

&#62;&#62;&#62; &#160;nonprim(161)

161= 7*23

161 = 23 *7 &#160;&#160;&#160;hurray!

Now look at this code from &#160;Python Tutorial 4.4 &#160;BREAK statements

&#62;&#62;&#62; for n in range(2, 10):

... for x in range(2, n):

... if n % x == 0:

... print n, 'equals', x, '*', n/x

... break

... else:

... # loop fell through without finding a factor

... print n, 'is a prime number'

...

2 is a prime number

3 is a prime number

4 equals 2 * 2

5 is a prime number

6 equals 2 * 3

7 is a prime number

8 equals 2 * 4

9 equals 3 * 3

</pre>

<p>&#160;

&#160;

see their explanation at 4.4...

so, &#160;when &#160;the &#160;n%x &#160;==0 succeeds, the number is NOT prime,

(do NOT just copy above...&#160;

&#160;&#160;&#160;MUST GET INDENTATION precisely correct!!!

&#160;

&#160;

this works:

&#160;

&#62;&#62;&#62; for n in range(2, 10):

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for x in range(2, n):

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if n % x == 0:

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;print n, 'equals', x, '*', n/x

&#160;

&#160;

4 equals 2 * 2

6 equals 2 * 3

6 equals 3 * 2

8 equals 2 * 4

8 equals 4 * 2

9 equals 3 * 3

&#160;

next I insert a BREAK... and it halts the INNERMOST FOR loop

&#160;

Note: &#160;ELSE must line up precisely with the innermost for loop which the

break... breaks out of... it DOES WORK!!!

&#160;

def primes():

&#160;&#160;&#160;&#160;&#160;&#160;for n in range(2, 10):

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for x in range(2, n):

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if n % x == 0:

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;print n, 'equals', x, '*', n/x

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;break

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;else:

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;print n, 'is a prime'

&#160;

(use spaces and not tabs, if possible... drives me nuts!)

&#160;

&#160;

&#160;

&#160;

&#160;

&#160;

&#160;

&#160;

&#160;

&#160;

&#160;</p>