I’ve been going through a Haskell tutorial (Just to see what it’s like)
and saw an example program which would tell me how long each side a
right-angle triangle is, when given the perimeter. It seemed pretty
simple, and me, being the kind of guy that likes to write the same
program in multiple languages, tried to write it in ruby.

I’m pretty stumped though, and would like someone a little higher up to
give me a nudge in the right direction :D.

(Eg. program would return (6, 8, 10) and (8, 6, 10) when you say that
the perimeter is 24)

We are in no way connected, in fact I don’t really know any other
programmers :O. So I would assume this is a glitch in the system. I hope
there is some way to merge these two threads together.

Now to reply to the questions:

Is this no good?
[7, 7.058823529, 9.941176471]

or this?
[6.5, 7.542857143, 9.957142857]

Are you requiring only integers?
Can you show some code?

Yea for simplicity’s sake I just want full integers, although I don’t
really mind either way.

Here is the Haskell code:
ghci> let rightTriangles’ = [ (a,b,c) | c ← [1…10], b ← [1…c], a ←
[1…b], a^2 + b^2 == c^2, a+b+c == 24]
ghci> rightTriangles’
[(6,8,10)]

And I don’t have any ruby code, that’s why I’m posting here! :3 Just
wondering how I would start about to make a program which uses
Pythagoras Theorem.

And I don’t have any ruby code, that’s why I’m posting here! :3 Just
wondering how I would start about to make a program which uses
Pythagoras Theorem.

A direct translation of that would look like this:

def right_triangles
results = []
(1…10).each do |c|
(1…c).each do |b|
(1…b).each do |a|
results << [a, b, c] if a+b+c == 24 && a2 + b2 == c**2
end
end
end
results
end

right_triangles # => [[6, 8, 10]]

Ruby doesn’t have list comprehensions like the Haskell example uses
(Python
might, not sure). If math functions and algorithms are your primary
domain,
then Haskell is a better choice than Ruby (Python is, too, for this
domain),
because it has libs and a community built around this use case.

Thanks guys! You’ve both been helpful! Now how would I make it so that
it would return a float? Perhaps up to two decimal places? Here’s my
code so far : http://pastie.org/2479748

I tried making the range into a float, but then I’m not sure how that
would work, at what decimal point range it would end up at, it gave me
an error anyways.

Thanks guys! You’ve both been helpful! Now how would I make it so that
it would return a float? Perhaps up to two decimal places? Here’s my
code so far : http://pastie.org/2479748

I tried making the range into a float, but then I’m not sure how that
would work, at what decimal point range it would end up at, it gave me
an error anyways.

Check my equations carefully to be sure they are correct.
But, it seems to work…
Anyway, it’s an idea.

per = 24 # 2_400_000
(1…per-2).each do |a|
c = (a.to_f**2+(per-a)*2)/(2(per-a))
b = per-a-c
p [a,b.to_i,c.to_i] if a>=1 && b>=1 && c>=1 && b == b.to_i # integers
only #p [a,b,c] if a>=1 && b>=1 && c>=1 #include floats
end

Thanks guys! You’ve both been helpful! Now how would I make it so that
it would return a float? Perhaps up to two decimal places? Here’s my
code so far : http://pastie.org/2479748

I tried making the range into a float, but then I’m not sure how that
would work, at what decimal point range it would end up at, it gave me
an error anyways.

Sorry for the repost.
Slight modifications.

integers

per = 24 # 2_400_000
(1…per).each do |a|
c = (a.to_f**2+(per-a)*2)/(2(per-a))
b = per-a-c
p [a,b.to_i,c.to_i] if a>=1 && b>=1 && c>=1 && b==b.to_i && c==c.to_i
end

puts

some floats

per = 24.3
(0…per).each do |a|
c = (a.to_f**2+(per-a)*2)/(2(per-a))
b = per-a-c
p [a,b,c] if a>0 && b>0 && c>0
end