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!

···

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

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:

let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <-

[1..b], a^2 + b^2 == c^2, a+b+c == 24]

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.

···

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

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.

···

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

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 by getting the
hypotenuse? I'm a little confused by the code :3. Thanks!

···

--
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 && a**2 + b**2 == 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.

···

On Sat, Sep 3, 2011 at 11:23 PM, Kane Williams <theburrick@hotmail.com>wrote:

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:
> let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <-
[1..b], a^2 + b^2 == c^2, a+b+c == 24]
> rightTriangles'
[(6,8,10)]

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/\.

try,

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

kind regards -botp

···

On Sun, Sep 4, 2011 at 12:23 PM, Kane Williams <theburrick@hotmail.com> wrote:

> let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <-
[1..b], a^2 + b^2 == c^2, a+b+c == 24]
> rightTriangles'
[(6,8,10)]

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 Williams <theburrick@hotmail.com> 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

···

On Sun, Sep 4, 2011 at 5:10 PM, Kane Williams <theburrick@hotmail.com> 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.

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
a**2 + b**2 = c**2
b = p-a-c
a**2 + (p-a-c)**2 = c**2
a**2 + p**2 - pa - pc - ap + a**2 + ac - cp + ca + c**2 = c**2
a**2 + (p**2 - 2ap + a**2) - 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

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

-- Matma Rex

···

2011/9/5 Kane Williams <theburrick@hotmail.com>:

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?

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

···

On Sun, Sep 4, 2011 at 6:43 AM, Josh Cheek <josh.cheek@gmail.com> wrote:

On Sat, Sep 3, 2011 at 11:23 PM, Kane Williams <theburrick@hotmail.com>wrote:

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:
> let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <-
[1..b], a^2 + b^2 == c^2, a+b+c == 24]
> 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.

--
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 && a**2 + b**2 == c**2
end
end
end
results
end

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/