-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
My solutions to problems 1-3 for Week 01 homework #15
base: master
Are you sure you want to change the base?
Conversation
|
||
#Sum the multiples of 3 or 5 that are below 1000. The range function does not include the last number, so only goes up to 999 | ||
# "%" is the modulus/remainder operator, if the remainder of a Number between 0 and 999 divided by 3 or 5 is equal to 0, then sum it. | ||
print(sum(i for i in range(0, 1000) if i % 3 == 0 or i % 5 == 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the most Pythonic line I've seen yet in anyone's submissions...nice compound statement!
|
||
previous, current = 0, 1 | ||
total = 0 | ||
while True: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though the while True: condition break
structure works, I'd counsel against writing loops this way because if you ever forget to write/have a bug in your break
statements, you'll get caught in an infinite loop that will crash your program. Instead, you can write
while current <= 4e6:
...
to achieve the same result as the first three lines of your loop.
n= 600851475143 | ||
factor = 2 | ||
|
||
while factor * factor < n: #bc largest prime factor will never be > the sqrt of n |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice logic in reducing the size of your loop!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd still like you to try 4 & 5 on your own and add them to this PR before I merge it in, but first take a look at this tutorial on functions in Python. Let me know if there's anything specific you're having trouble understanding and I'll do my best to help!
Description
Scripts with solutions to the first 3 Euler problems
Weekly Journal
I couldn't get very far trying the solutions completely on my own so I ended up finding solutions on google. I made sure to go through each step and try to understand what was happening. I found solutions for problems 4 and 5 that spit out the correct answer when I ran them, but I didn't really fully understand what was happening at each step and so I did not include them with my submission here. I find that I am fine with using simple, if, while, and for loops; my issue is that I do not fully understand how functions work.
Questions