Function Use: To print all the directories in a folder/sub-folder or directory import os def get_list_or_dir(local_dir): for dir_or_file in os.listdir(local_dir): if os.path.isdir(dir_or_file): path = os.path.join(local_dir, dir_or_file) print(dir_or_file, 'This is a directory') get_list_or_dir(path) else: print(dir_or_file) Path = os.getcwd() or define the path from where you need a file Function Call: get_list_or_dir(path)
I have got this difficult programming ask from Project Euler. Before jumping into details we should know what is the prime number and prime number factors..
Prime Number:
The number which is divisible only by itself and 1.
Example: 1, 3, 5, 11, 13 and so on. These are prime number.
Is 9 a prime number? No, 9 is divisible by 1, 3, 9. So, it can't be a prime number.
A program to print prime number till 100.
>>> n =100
>>> i = 2
>>> for i in range(2, n+1):
count = 0
for j in range(2, i+1):
if i%j == 0:
count += 1
if count == 1:
print(i, end=" ")
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97.
What is the prime factor?
The factor of any number which is a prime number then it is a prime factor.
Example: what is the prime factor of 100: The prime factor of 100 is (2,2,5,5); where 2 and 5 is a prime number? How did we came to the prime factor.
We can start from 2 to the n(for which we have to find prime factor)
Now, come to the original question!
What is the largest prime factor of the number 600851475143 ?
>>> n = 600851475143
>>> a= []
>>> i = 2
>>> while i < 100001:
if n%i == 0:
n = n/i
a.append(i)
i = 2
else: i += 1
>>> a[len(a) -1]
6857
Here the prime factor of 600851475143 is ( 71, 839, 1471, 6857). Here i have used an list to keep these and the last value of the list of largest prime factor.
Keep reading Python an hour a day to learn simple Python tricks, tips and program.
Prime Number:
The number which is divisible only by itself and 1.
Example: 1, 3, 5, 11, 13 and so on. These are prime number.
Is 9 a prime number? No, 9 is divisible by 1, 3, 9. So, it can't be a prime number.
A program to print prime number till 100.
>>> n =100
>>> i = 2
>>> for i in range(2, n+1):
count = 0
for j in range(2, i+1):
if i%j == 0:
count += 1
if count == 1:
print(i, end=" ")
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97.
What is the prime factor?
The factor of any number which is a prime number then it is a prime factor.
Example: what is the prime factor of 100: The prime factor of 100 is (2,2,5,5); where 2 and 5 is a prime number? How did we came to the prime factor.
We can start from 2 to the n(for which we have to find prime factor)
- 100/2 = 50 (2 is a prime factor of 100)
- 50/2 = 25 (2 is a prime factor of 100)
- 25/5 = 5 (Here 25 is not divisible by 2, then we will go to the next prime number which is 3. Again 25 is not divisible by 3, then we will go to the next prime number which is 5. Now, 25 is divisible by 5.)
- 5/5 = 1 (5 is a prime factor and we will stop here, as we can't go further then 1)
Now, come to the original question!
What is the largest prime factor of the number 600851475143 ?
>>> n = 600851475143
>>> a= []
>>> i = 2
>>> while i < 100001:
if n%i == 0:
n = n/i
a.append(i)
i = 2
else: i += 1
>>> a[len(a) -1]
6857
Here the prime factor of 600851475143 is ( 71, 839, 1471, 6857). Here i have used an list to keep these and the last value of the list of largest prime factor.
Keep reading Python an hour a day to learn simple Python tricks, tips and program.
Comments
Post a Comment