I have some IT homework that I'm struggling a bit with, and some pointers would be greatly appreciated.
**Please not: I'm not asking for someone to write the program for me, just for some pointers as where to start as I'm feeling a bit blank at the moment.
The question is as follows:
IT Q wrote:The positive integers a, b and c are called a "Pythagorian triplet" if
a*a + b*b = c*c
For example, 3*3 + 4*4 = 5*5, so (3, 4, 5) is a Pythagorian triple.
There is only one Pythagorian triplet (a, b, c) such that a < b < c and
a + b + c = 1000.
Write a Java program to find this a, b and c, and write the number
a*b*c
Thanks in advance
AMD Tri-Core 720BE @ 3.5Ghz
Biostar TA790GX A3+
4gig OCZ Platinum DDR3-1333
ATI HD4890 @ 1050Mhz Mem / 900Mhz Core
You can brute force it. Work out all the c values in the triples less than say, 700. Then add a, b and c and which ever triple adds up to 1000 is the one you want.
(when you've done that, look through your program and try make it more efficent)
Art Williams wrote:I'm not telling you it is going to be easy, I'm telling you it's going to be worth it.
Hex_Rated wrote:You need to use 3 nested loops (actually 2 nested inside a main one) and do the comparisons until you find the set that satisfies the parameters.
I wish i still did java. I love the problem solving aspect.
Another trick is to try simplifying it on paper, get rough 'sketch' of your program down, it makes it a lot easier to write it (this works very well with problems involving maths)
Art Williams wrote:I'm not telling you it is going to be easy, I'm telling you it's going to be worth it.
import javax.swing.*;
public class rawrHW1 {
public static void main (String[] args)
{
int a;
int b;
int c;
int abc;
int test;
for (c=3; c<=1000; c++){
for (b=2; b<=c; b++){
for (a=1; a<=b; a++){
test = a+b+c;
if (test ==1000 && (a*a)+(b*b)==(c*c)){
abc = a*b*c;
System.out.println ("a = " + a + "\nb = " + b + "\nc = " + c + "\na*b*c = " + abc);
}
}
}
}
}
}
PS - Don't worry, I haven't spent the entire day on this little bit, I've only been looking at it for like 10min at a time.
Last edited by pienkie on 01 Feb 2009, 22:26, edited 1 time in total.
AMD Tri-Core 720BE @ 3.5Ghz
Biostar TA790GX A3+
4gig OCZ Platinum DDR3-1333
ATI HD4890 @ 1050Mhz Mem / 900Mhz Core
TheModDoctor wrote:I wish our school did java. We only do Borland Delphi 7 which is very simple and easy to use, but I think that java is way more popular.
We also did Delphi, was okay, but if I knew what I know now in Gr10 I'd never have taken it.
You must start at 3 4 5 not 1 2 3. But otherwise it will work.
You could use a constant in the comparator of the b and c loop for optimization (comparing variable to variable is slower than comparing variable to constant as the program has to fetch the value out of memory each time) and break the loop when you get the answer but your way is fine as well.
Statistically you only need to take c up to somethinmg like 500 or 750 besides that you can actually work this with a single while loop.
Will post C# code after I tested though.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
button1.Enabled = false;
this.Text = "BUSY";
int a = 1, b = 2, c = 3;
Boolean found = false;
while (!found)
{
if (a + 1 >= b) { b++; a = 1; }
if (b + 1 >= c) { c++; b = 2; a = 1; }
a++;
if (a + b + c == 1000)
{
this.Text = "BUSY [" + a + "|" + b + "|" + c + "]";
if (a * a + b * b == c * c)
{
DateTime end = DateTime.Now;
this.Text = "DONE [" + a + "|" + b + "|" + c + "] = " + end.Subtract((DateTime)start);
found = true;
}
}
Application.DoEvents();
}
}
}
}
I wish we did JAVA. Java is not too different than c#, which I am quite familiar with. Our school teached Delphi, I mean honestly, Delphi for crying out loud