Beginner needing help - Writing right-angle triangle program

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)

Thanks in advance!

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)

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?

Harry

Thanks for the reply!

I wanted to get something out of the way first, some other guy made a
thread exactly the same as my one and it has generated 6 replys somehow.
It is located here Beginner needing help - Writing right-angle triangle program - Ruby - Ruby-Forum although
you can probably just see it for yourself on the frontpage.

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)]

Which was found at the bottom of this tutorial:
http://learnyouahaskell.com/starting-out#tuples

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.

On Sat, Sep 3, 2011 at 11:23 PM, Kane W.
[email protected]wrote:

Can you show some code?
Which was found at the bottom of this tutorial:
Starting Out - Learn You a Haskell for Great Good!

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.


Posted via http://www.ruby-forum.com/.

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.

On Sun, Sep 4, 2011 at 12:23 PM, Kane W. [email protected]
wrote:

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)]

try,

(1…10).to_a.permutation(3).select{|a,b,c| a+b+c==24 && a2+b2==c**2}
#=> [[6, 8, 10], [8, 6, 10]]

kind regards -botp

On Sun, Sep 4, 2011 at 5:10 PM, Kane W. [email protected]
wrote:

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

Harry

On Sun, Sep 4, 2011 at 5:10 PM, Kane W. [email protected]
wrote:

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

Harry

Can you explain the final part of your code? Where it says:

p [a,b,c] if a>0 && b>0 && c>0
What is p? Can you just use it instead of puts?

And uh, this isn’t really programming, but how did you go about getting
the
hypotenuse? I’m a little confused by the code :3. Thanks!

On Sun, Sep 4, 2011 at 6:43 AM, Josh C. [email protected] wrote:

programmers :O. So I would assume this is a glitch in the system. I hope

[(6,8,10)]

end
end
results
end

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

I’d rather stick with a more traditional approach

for c in 1 … perimeter / 2
cq = c * c

for b in 1 … c
a = perimeter - c - b
p [a, b, c] if a * a + b * b == cq
end
end

Note: the third side can be calculated from the first two sides so
iterating is not necessary for that.

Kind regards

robert

2011/9/5 Kane W. [email protected]:

Can you explain the final part of your code? Where it says:

p [a,b,c] if a>0 && b>0 && c>0
What is p? Can you just use it instead of puts?

p var is mostly the same as puts var.inspect.

– Matma R.

And uh, this isn’t really programming, but how did you go by getting the
hypotenuse? I’m a little confused by the code :3. Thanks!

p = perimeter
a2 + b2 = c2
b = p-a-c
a
2 + (p-a-c)2 = c2
a2 + p2 - pa - pc - ap + a2 + ac - cp + ca + c2 = c2
a
2 + (p2 - 2ap + a2) - 2c(p-a) = 0
2c(p-a) = a**2 + (p-a)2
2c = (a
2 + (p-a)2) / (p-a)
c = (a
2 + (p-a)**2) / 2(p-a)

Harry