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 ?  Problem 3
 
Feb 13, 2013 08:47 PM
 
key idea is to divide it down as you test each prime!
 
intermediate steps...
 
primes = [2,3,5,7,11,13,17,19,23]
 
this works  (see list comprehensions in for loops... note:  NO fking  colon!)
 
def primes():
      numbers = [i for i in range (25) if i in primes]
      print numbers
 
#note: NO colon above  in the for loop!!!
 
below works:
#cand is a candidate
def nonprim(cand):
      for p in primes:
             if cand%p ==0:
                    print cand,"=",p,"*",int(cand/p)
 
>>>  nonprim(161)
161= 7*23
161 = 23 *7    hurray!
Now look at this code from  Python Tutorial 4.4  BREAK statements
>>> 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> 
 
see their explanation at 4.4...
so,  when  the  n%x  ==0 succeeds, the number is NOT prime,
(do NOT just copy above... 
   MUST GET INDENTATION precisely correct!!!
 
 
this works:
 
>>> for n in range(2, 10):
            for x in range(2, n):
                if n % x == 0:
                print n, 'equals', x, '*', n/x
 
 
4 equals 2 * 2
6 equals 2 * 3
6 equals 3 * 2
8 equals 2 * 4
8 equals 4 * 2
9 equals 3 * 3
 
next I insert a BREAK... and it halts the INNERMOST FOR loop
 
Note:  ELSE must line up precisely with the innermost for loop which the
break... breaks out of... it DOES WORK!!!
 
def primes():
      for n in range(2, 10):
          for x in range(2, n):
                 if n % x == 0:
                        print n, 'equals', x, '*', n/x
                        break
          else:
             print n, 'is a prime'
 
(use spaces and not tabs, if possible... drives me nuts!)
 
 
 
 
 
 
 
 
 
 
 </p>