-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP003.py
More file actions
34 lines (33 loc) · 1008 Bytes
/
P003.py
File metadata and controls
34 lines (33 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
#===============================================================================
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?
#===============================================================================
import math
def isPrime(n):
'''check if integer n is a prime'''
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
# all other even numbers are not primes
if not n & 1:
return False
# range starts with 3 and only needs to go up the squareroot of n
# for all odd numbers
for x in range(3, int(math.sqrt(n))+1, 2):
if n % x == 0:
return False
return True
#
#pNum=0
#for nn in range(150000):
# if isPrime(nn):
# pNum += 1
# print pNum, nn
# if pNum == 10001:
# break