December 22, 2024, 01:00:00 PM

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

guff

okay did a ruby version too akudood;
Code Select

total = 0
for i in 1...1000 do total += i if [i % 3, i % 5].include? 0 end
puts total


gosh i gave up on ruby a while ago for python but on using it again i love its syntax so very much
it just needs list comprehensions

i also realized i ended up writing a sum function because i don't think ruby has one built in:
Code Select
module Sum
def sum
total = 0
for element in self do total += element end
total
end
end

class Array
include Sum
end


then i realized that my code didn't even use it but if it did it would have looked pretty n_u

guff

okay jmv's supposedly been trying to learn clojure so i decided to try and make him look silly by solving problem one in it while having no idea how to do anything in it  baddood;

Code Select
((def sum 0)
(loop [i 0]
(when (< i 1000)
(if ((fn [x] (or (= 0 (rem x 3)) (= 0 (rem x 5)))) i) (def sum (+ sum i)))
(recur (inc i))))
(println sum))


it prints out the correct answer
but it also encounters some catastrophic errors or something at the end i dunno at least it gives the answer  akudood;

granted all i had to go on was skimming through this small tutorial so i had no idea how to do, say foo += bar all lisp-like so instead i had to do (def foo (+ foo bar)) and i'm pretty sure there's got to be a better way
i should also probably try and figure out why the loop explodes but there really doesn't seem to be that many tutorials for clojure floating around  akudood;

Gladjaframpf

People who use looping constructs in functional languages go to SUPER HELL. akudood;

I don't know much about Clojure specifically, but since it's a Lisp dialect I'd recommend making a recursive function that takes a parameter n and counts down from n to zero. Then you can just call the function with 999.

guff

Quote from: Gladjaframpf on January 27, 2009, 06:44:34 PM
People who use looping constructs in functional languages go to SUPER HELL. akudood;

I don't know much about Clojure specifically, but since it's a Lisp dialect I'd recommend making a recursive function that takes a parameter n and counts down from n to zero. Then you can just call the function with 999.
yeah probably but recall that this was hastily done in five minutes with no prior knowledge of clojure or any lisp for that matter baddood;

i'll just stick to haskell for my future functional programming needs screw you and your strict evaluation  akudood;

Gladjaframpf

Quote from: guff on January 27, 2009, 06:50:08 PM
yeah probably but recall that this was hastily done in five minutes with no prior knowledge of clojure or any lisp for that matter baddood;

i'll just stick to haskell for my future functional programming needs screw you and your strict evaluation  akudood;


Yeah I looked at the tutorial you were working from and it sucked so I don't blame you. I guess since it's designed for non-Lisp programmers it's trying to demonstrate things that someone used to imperative languages would recognize, but that's kind of silly because the whole point of learning languages with different paradigms is to explore new ways of doing things.

guff

okay after looking a little more into the language i was able to write this (also i didn't look at your scheme solution until after i posted this):
Code Select
(println (reduce + (filter (fn [i] (or (= (rem i 3) 0) (= (rem i 5) 0))) (range 1000))))

happy now  akudood;

if i am going to get into any lisp dialect i need an editor with automatic parentheses matching that isn't emacs or vim because i don't want a whole shit damn operating system  akudood;

Gladjaframpf

Yes that is much better.

I noticed something sort of odd when I was doing that problem. Simply looping through the numbers 1 to 999 should be faster than my solution, which first makes a list of the numbers and then adds them up so it has to go through all the numbers twice. However, tests show that my solution is faster. I guess the built-in functions for creating and folding lists are so heavily optimized that it makes up for it.

Daddy

Yo dawg, I heard you like least common multiples.
Code Select
#!/usr/bin/python
# Filename : Euler_005.py
#What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?
def gcd(a, b):
while b != 0:
  t =b
  b = a%b
  a = t
return a
def lcm(a,b):
d = a*b/gcd(a,b)
return d
print lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(1,2),3),4),5),6),7),8),9),10),11),12),13),14),15),16),17),18),19),20)


is there a way to get it so I don't need 1000000000000000 LCMs?

guff

Quote from: Jacques Michel Valente on January 29, 2009, 08:45:13 PM
Code Select

print lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(lcm(1,2),3),4),5),6),7),8),9),10),11),12),13),14),15),16),17),18),19),20)

OH MY GOD  akudood;

there's more than one way to do it
you could use the reduce function, which takes a function and an iterable object as input so you could just do something like:
Code Select
reduce(lcm, range(1, 21))

or, you could just use a for loop like this:
Code Select
current_lcm = 1
for i in range(1, 21):
current_lcm = lcm(current_lcm, i)


the for loop might be a better idea if you intend to learn python3.0 because they moved reduce from the builtins to the functools module akudood;

Feynman


Daddy


Feynman

January 30, 2009, 12:52:35 PM #86 Last Edit: January 30, 2009, 12:56:38 PM by Bassir
Quote from: Jacques Michel Valente on January 30, 2009, 11:57:55 AM
Can't copypaste these problems?  akudood;


i don't know python :'(

but i after seeing their neat logo i may consider it

guff

Quote from: Bassir on January 30, 2009, 12:52:35 PM
i don't know python :'(
it takes all of five minutes to learn   baddood;

UNLESS YOU ARE JMV  akudood;

Daddy

Quote from: guff on January 30, 2009, 01:12:30 PM
it takes all of five minutes to learn   baddood;

UNLESS YOU ARE JMV  akudood;
then it takes java

ncba93ivyase

Just decided to do problem 9:
Code Select
for a in range(1, 1000):
for b in range(1, 1000):
c = ((a**2)+(b**2))**.5
if a+b+c == 1000:
print a*b*c
exit()

it's an ugly way of doing it, but i got it right the first try

and i threw exit in there so it'd quit as soon as it found the solution instead of going for that tenth of a second more to solve it a second time

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

Go Up