+ 8

Sololearn is struggling to run my code

Hi guys 👋. I made a code to calculate the largest prime factor of a number and it works fine for small numbers like 13195 but i wanna know the largest prime factor of 600851475143 but when i run it shows no output. I get that sololearn is struggling to run my code. But i really need to know the ans. The code: https://code.sololearn.com/ctAVPBDEwPUB/?ref=app I think if the code is run in a powerful computer then I'll get the output. But I don't have one.

20th Jul 2020, 7:29 AM
MSN
MSN - avatar
24 Answers
+ 10
For a free cpu/gpu resource, you can try google colab, which is a jupyter notebook environment which can run your Python code in cloud. https://colab.research.google.com
20th Jul 2020, 7:42 AM
李立威
+ 9
The prime factors of 600851475143 are [839, 71, 6857, 1471], so the largest prime factor is 6857. If you multiply all factors, you will get the input value: 839 * 71 * 6857 * 1471 == 600851475143 [edited]: 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ,To get prime factors of a value, you can use the sympy function primefactors() as demonstrated here: from sympy import primefactors n = 600851475143 pr_facts = primefactors(n) print(f'prime factors of {n} are {pr_facts}') # result is [71, 839, 1471, 6857] if you are forced to do it without module, the algorithm has to be adapted for max performance as already mentioned.
20th Jul 2020, 11:23 AM
Lothar
Lothar - avatar
+ 9
Namit Jain , you are absolutely right when you talk about learning and enhancing problem solving ability. This a must for all coders. But doing profession coding is something different. I always would rely on well-known and long-term supported modules. Modules like sympy, numpy and so on, has highly optimized code. So why should i bark myself, when i have a dog who can do this better? 🙂
20th Jul 2020, 2:07 PM
Lothar
Lothar - avatar
+ 7
Yaah!! This happens many times even with recursions!
20th Jul 2020, 7:42 AM
Namit Jain
Namit Jain - avatar
+ 6
Namit Jain , yes - this was a typo and i have corrected it. Shame on me! 😁
20th Jul 2020, 2:11 PM
Lothar
Lothar - avatar
+ 5
Lothar suppose a person doesn't know about sympy then? There should be an algorithm that can do this for you! Using py libraries don't enhance your problem solving ability 🤔 Making simplest and fastest ways to find answer: https://code.sololearn.com/cH6Q6abXF9yC/?ref=app
20th Jul 2020, 1:24 PM
Namit Jain
Namit Jain - avatar
+ 3
If you want to further accelerate the calculation, you may want to change your algorithm. There are many ways to find prime numbers (not all of them are straightforward). Take a look at some related articles: e.g. https://stackoverflow.com/q/453793
20th Jul 2020, 8:01 AM
李立威
20th Jul 2020, 8:03 AM
Namit Jain
Namit Jain - avatar
+ 3
Thanks 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 sir! 😊😊😊 can u explain your code pls
20th Jul 2020, 8:56 AM
MSN
MSN - avatar
+ 3
Nice explanation 👌👌👌 Thanks 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥
20th Jul 2020, 9:26 AM
MSN
MSN - avatar
+ 3
NUHEEN ohh sorry!! It should be a prime factor! I forgot
20th Jul 2020, 9:35 AM
Namit Jain
Namit Jain - avatar
+ 3
NUHEEN now check it! Can be said as one of the easiest way to do this problem! Still some problem?
20th Jul 2020, 9:46 AM
Namit Jain
Namit Jain - avatar
+ 3
Nicely done Namit Jain
20th Jul 2020, 9:47 AM
MSN
MSN - avatar
+ 3
Checking if a number is prime by dividing by all numbers from 2 to n - 1 takes O(n) time. You can iterate up to math.sqrt(n) and reduce your time to O(sqrt(n)) which is far faster. There are lots of optimizations possible with checking for primes but just that O(sqrt(n)) optimation is enough to calculate the result for this in a fraction of a second. Here is an optimized version that runs in a fraction of a second: import math def is_prime(n): max_i = int(math.sqrt(n)) if max_i * max_i == n: return False # perfect squares are not prime. else: for i in range(2, max_i): if n%i==0: return False return True def largest_prime_factor(n): factors = [] max_i = int(math.sqrt(n)) if is_prime(max_i) and max_i * max_i == n: return max_i for i in range(1,max_i): if n%i==0: if is_prime(i): factors.append(i) return max(factors) print(largest_prime_factor(600851475143))
21st Jul 2020, 12:46 AM
Josh Greig
Josh Greig - avatar
+ 2
李立威 i tried colabatory according to u but it's taking forever to show the output! Any other way?
20th Jul 2020, 7:53 AM
MSN
MSN - avatar
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 sir can u help me solve this problem?
20th Jul 2020, 8:09 AM
MSN
MSN - avatar
+ 2
NUHEEN Easy algorithm! Anyone can make it in just 2 minutes! No modules imported https://code.sololearn.com/cH6Q6abXF9yC/?ref=app
20th Jul 2020, 9:31 AM
Namit Jain
Namit Jain - avatar
+ 2
But Namit Jain your code doesn't give the correct output... 13195 should output 29
20th Jul 2020, 9:34 AM
MSN
MSN - avatar
+ 2
NUHEEN Sorry for the wrong code at the beginning! 'PRIME' just slipped off my mind!
20th Jul 2020, 9:49 AM
Namit Jain
Namit Jain - avatar
+ 2
Lothar shouldn't it be 6857
20th Jul 2020, 11:27 AM
Namit Jain
Namit Jain - avatar