December 22, 2024, 02:05:16 AM

1,531,361 Posts in 46,734 Topics by 1,523 Members
› View the most recent posts on the forum.


I decided to step up to Guff's challenge

Started by ncba93ivyase, January 19, 2009, 11:18:13 PM

previous topic - next topic

0 Members and 2 Guests are viewing this topic.

Go Down

ncba93ivyase

January 19, 2009, 11:18:13 PM Last Edit: January 19, 2009, 11:32:50 PM by Pancake Persona
And managed to solve the easiest Project Euler problem. badass

>>> x = 0
>>> for i in range(1, 1000):
...     if i % 3 == 0 or i % 5 == 0:
...             x+=i
...
>>> x
233168


I noticed wikipedia had another solution and compared my answer to theirs, and yes, it's right.

I SAY WE SHOULD ALL WORK TOWARDS SOLVING ALL OF THESE.

Also, I've been trying to think of a simple and efficient way to solve problem 5, and I keep getting answers that I know are wrong. :'(

and the website in case you're too stupid to google it: http://projecteuler.net/index.php?section=problems

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

January 20, 2009, 07:06:39 AM #1 Last Edit: January 24, 2009, 05:10:03 PM by guff
better than jmv could do badass

also my naive solution for number one is sum([i for i in range(1000) if 0 in (i%3, i%5)]) which is much cooler than yours
the mathy way to do it would be to make use of the fact that the sum of the first n natural numbers is [tex]\frac{n(n+1)}{2}[/tex] so the sum of the multiples of 3 that are less than 1000 would be [tex]3*\frac{n(n+1)}{2}[/tex] where n is [tex]\lfloor \frac{1000}{3} \rfloor = 333[/tex]
so we do the same for five, and those two sums, then subtract all the multiples of 15
that's less work for the computer but also lots more work for the coder so yeah i'll just stick with the first one  baddood;

also my profile
i haven't solved any new ones for a bit but for the last couple days i've been working my way through them again and actually keeping all my files and solutions organized this time for future reference

Bolivian Army

guff is nerding up this place too i see  baddood;

i'll do one when i get home

Donate now to the Guff Is Great foundation. baddood;

guff

okay i've redone the first ten now and have decent (i.e. less than two seconds to run) solutions for each

also number five is easy peasy if you use euclid's algorithm bassir;

ncba93ivyase

Quote from: andrƃĀ© the giant on January 20, 2009, 09:40:07 AM
okay i've redone the first ten now and have decent (i.e. less than two seconds to run) solutions for each

also number five is easy peasy if you use euclid's algorithm bassir;
ahhhhh i hate you :'(

I MUST TRY AGAIN

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

ncba93ivyase

I think I've come up with a good solution for number 20. Here's what I typed up, but it's not going to give you the actual answer (of course):
Code Select
x = str(25373)
g = 0
n = 0
for items in x:
q = int(x[g:g+1])
n+=q
g+=1
print n

I tested it and it works perfectly fine. Didn't include the factorial part because if you can't do that (JMV baddood; ), you shouldn't even bother using a computer.

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

Quote from: Pancake Persona on January 21, 2009, 04:13:14 AM
Code Select

q = int(x[g:g+1])

wat goonish

for strings, blah[foo : foo + 1] returns exactly the same thing that blah[foo] does

also you should probably learn how to use list comprehensions because then you could do:
Code Select
def sum_of_digits(numbar):
    return sum([int(digit) for digit in str(numbar)])


or at the very least slim down your for loop a bit by eliminating g:
Quote from: Pancake Persona on January 21, 2009, 04:13:14 AM
Code Select
x = str(25373)
total = 0
for digit in x:
total += int(digit)
print total



problem 20 is easy for languages with built in bignum support (e.g. python, ruby, and every other language that doesn't SUCK  madood;) but 100! is a pretty big number and it would definitely be a bit harder to do this in c


ncba93ivyase

Quote from: andrƃĀ© the giant on January 21, 2009, 06:05:34 AM
wat goonish

for strings, blah[foo : foo + 1] returns exactly the same thing that blah[foo] does
\

or at the very least slim down your for loop a bit by eliminating g:
what matters most is i got it done, albeit through unconventional methods

I just wonder if JMV could actually do any of them

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

Quote from: Pancake Persona on January 21, 2009, 12:57:22 PM
what matters most is i got it done, albeit through unconventional methods

I just wonder if JMV could actually do any of them
well yeah but given that you've been coding games you should probably have better style  baddood;

he did the first one but that was with a lot of help  akudood;
maybe he could do it in java n_u

ncba93ivyase

Quote from: andrƃĀ© the giant on January 21, 2009, 01:01:53 PM
well yeah but given that you've been coding games you should probably have better style  baddood;

he did the first one but that was with a lot of help  akudood;
maybe he could do it in java n_u
games are just heaps and mounds of if statements and not much else, so they really don't help me learn and comprehend everything that i should know by now which i'll admit is shameful goonish

for example, a randomly selected chunk from my most recent project:
Code Select
if item.falling == False:
item.y += move_y
#If you reach 28 pixels above the starting point, fall
if item.y <item.y_init-30:
item.falling = True
#If you walk off the edge of a platform, fall
if item.y == item.y_init and my_color[0] > 0:
item.falling = True
#If falling... fall. If touching the ground, bring back ability to jump
if item.falling == True:
item.y +=1
if item.y >200:
item.y = 202
item.falling = False
item.y_init = item.y
#If you're on a platform, cease falling
if my_color[0] == 0 and my_color[2] == 0:
item.falling = False
item.y_init = item.y
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
if event.key == K_w:
move_y = -2
# X controls are inverted because the level moves, not the character.
if event.key == K_d:
move_x = -1
if event.key == K_a:

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

Quote from: Pancake Persona on January 21, 2009, 01:06:25 PM
games are just heaps and mounds of if statements and not much else, so they really don't help me learn and comprehend everything that i should know by now which i'll admit is shameful goonish
okay so read the official docs  baddood;

Quote from: Pancake Persona on January 21, 2009, 01:06:25 PM

for example, a randomly selected chunk from my most recent project:[spoiler]
Code Select
if item.falling == False:
item.y += move_y
#If you reach 28 pixels above the starting point, fall
if item.y <item.y_init-30:
item.falling = True
#If you walk off the edge of a platform, fall
if item.y == item.y_init and my_color[0] > 0:
item.falling = True
#If falling... fall. If touching the ground, bring back ability to jump
if item.falling == True:
item.y +=1
if item.y >200:
item.y = 202
item.falling = False
item.y_init = item.y
#If you're on a platform, cease falling
if my_color[0] == 0 and my_color[2] == 0:
item.falling = False
item.y_init = item.y
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
if event.key == K_w:
move_y = -2
# X controls are inverted because the level moves, not the character.
if event.key == K_d:
move_x = -1
if event.key == K_a:
[/spoiler]
okay well that mostly seems okay akudood;

FAMY2


guff


guff

okay how many you got now  baddood;

i haven't done any new ones yet but i did just redo number 12
my solution takes about 3.5 seconds to run though  saddood;

FAMY2


Go Up