Largest Prime Factor of 600851475143

  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Prime
Click For Summary
The discussion focuses on finding the largest prime factor of the number 600851475143 using Python programming. The initial code attempts to identify prime factors but produces incorrect outputs, such as including non-prime numbers like 9 and 33. Suggestions include writing pseudocode to clarify logic and using the square root method to optimize the search for factors, as checking all numbers up to N is inefficient. The conversation emphasizes the importance of understanding loops and conditions in programming, particularly the use of the break statement to exit loops early when a factor is found. Ultimately, a more efficient approach is recommended, limiting checks to divisors up to the square root of the target number.
  • #31
Vanadium 50 said:
Your refusal to write pseudocode is a mistake. Just sayin'.
I am writing and I am thinking about it. Just when I write the codes things happen.
 
Physics news on Phys.org
  • #32
I found it. Not in a direct way but I find it anyways
my code

Python:
N=600851475143
b=1
while True:
    if N%b==0:
        t=N//b
        b=b+1
        print (t)
    else:
        b=b+1
 
  • #33
Arman777 said:
Another sad thing is this is the 3rd problem in the project euler site
Sorry you're having such a time, but thanks for sharing the website,
https://projecteuler.net

I think that's just what I need for some practice problems.
I'm on my phone and I did the 1st one on my phone using Google Sheets while my kid is at swim practice .
:smile: not everything has to be done in Python
 
  • Like
Likes Arman777
  • #34
Vanadium 50 said:
Your refusal to write pseudocode is a mistake. Just sayin'.
Or a flowchart, perhaps.
I used a spreadsheet to solve this. Maybe I'll go try to write some code. I'm taking an online Python right now, so I think the extra practice will be good. But often you can use other tools.

My approach was to go along till I hit a factor, check to see if that number is prime. Take the original number 600851475143 and divide by that factor. Now I have a somewhat smaller number to find factors of. The next number that turns up to be a factor, divide by that one and I have a smaller number.
If you keep track of the product of these factors you've been saving, then you will know when to stop.
 
Last edited:
  • #35
scottdave said:
Sorry you're having such a time, but thanks for sharing the website,
https://projecteuler.net

I think that's just what I need for some practice problems.
I'm on my phone and I did the 1st one on my phone using Google Sheets while my kid is at swim practice .
[emoji2] not everything has to be done in Python
Yes and I actually come here to thank @StoneTemplePython for introducing me to the site, but I saw your post so your welcome somehow :)

I come to the 9th problem ( I solved the earlier ones as well) and I'll try to do more and more, Its really great indeed.

scottdave said:
Or a flowchart, perhaps.
I used a spreadsheet to solve this. Maybe I'll go try to write some code. I'm taking an online Python right now, so I think the extra practice will be good. But often you can use other tools.

My approach was to go along till I hit a factor, check to see if that number is prime. Take the original number 600851475143 and divide by that factor. Now I have a somewhat smaller number to find factors of. The next number that turns up to be a factor, divide by that one and I have a smaller number.
If you keep track of the product of these factors you've been saving, then you will know when to stop.
Well, I guess that's a good approach, the interesting thing is 4th, 5th or 6th problems are much easier than the 3rd one somehow (at least for me).
7th took some time to solve but I manage to do it.
8th is really nice :) If you are doing good or want to share your code (after solving) I can also share mine.

Edit:Also I should mention that first 100 problem is catagorized as easy so I am going slowly :)
 
Last edited:
  • #36
scottdave said:
.
I'm on my phone and I did the 1st one on my phone using Google Sheets while my kid is at swim practice .

scottdave said:
I'm taking an online Python right now, so I think the extra practice will be good. But often you can use other tools.

Btw, if you have an iPhone and/or iPad, I'd strongly recommend getting Pythonista. You can run Python scripts with any of the standard module imports (plus numpy if you want) and it runs great on both. I don't think its available on Android, unfortunately.

A few years ago, I would spend a lot of time on iPhone or iPad coding via Pythonista while waiting for train or bus (and sometimes while riding the train too depending on crowding). Getting an extra 20 - 40 mins in every day during waiting time really adds up in terms of scaling the learning curve.
- - - -
High level: my general approach is scribble something down with pen and paper first, then straight to Python. I don't use Pythonista much these days but I have fond memories.
 
  • Like
Likes scottdave
  • #37
Arman777 said:
I come to the 9th problem ( I solved the earlier ones as well) and I'll try to do more and more, Its really great indeed.

Well, I guess that's a good approach, the interesting thing is 4th, 5th or 6th problems are much easier than the 3rd one somehow (at least for me).
7th took some time to solve but I manage to do it.
8th is really nice :)
I haven't gone exactly in order. I'll work on one for a little while, then move on to another if I feel I'm stuck, then come back to revisit. These are the ones that I've solved so far: 1 thru 3, 5 thru 11, 13, 16, 19, 20, 22, 26, and 29
 
  • #38
scottdave said:
I haven't gone exactly in order. I'll work on one for a little while, then move on to another if I feel I'm stuck, then come back to revisit. These are the ones that I've solved so far: 1 thru 3, 5 thru 11, 13, 16, 19, 20, 22, 26, and 29
Thars great :) Sure you can
 
  • #40
Fred Wright said:
I haven't seen any problem where those are necessary. I found a 450-line version of the quadratic sieve online.
Implementing that by yourself is probably harder than any projecteuler problem.
Pollards_rho_algorithm is simple enough, if you think you need it.

What is useful many problems is generating a list of primes with https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
You can easily go up to 10^7 in python in a second or so. You could also store the smallest factor of each number in the sieve. This will get you instantaneous factoring.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 80 ·
3
Replies
80
Views
9K
  • · Replies 32 ·
2
Replies
32
Views
4K
Replies
4
Views
2K
  • · Replies 28 ·
Replies
28
Views
4K
Replies
33
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
22
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K