GR11 Java Question

Get help on programming - C++, Java, Delphi, etc.
Locked
pienkie
Registered User
Posts: 1168
Joined: 17 Apr 2007, 02:00
Contact:

GR11 Java Question

Post by pienkie »

Afternoon...,

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

3DMark06: 16 529
Monty
Forum Moderator
Posts: 10000
Joined: 05 Feb 2004, 02:00
Processor: Intel i5-4690K @ 4.5GHZ
Motherboard: ASUS Maximus VII Formula
Graphics card: ASUS GTX970 Strix
Memory: 4 x 4GB Corsair Dominators
Location: Messing with your Mind
Contact:

Re: GR11 Java Question

Post by Monty »

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
Registered User
Posts: 3679
Joined: 19 Jan 2006, 02:00
Contact:

Re: GR11 Java Question

Post by Hex_Rated »

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.

Code: Select all

for( ... {
   for( ... {
       for( ... {
       }
    }
}
DFI LanParty X48 LT-2TR
Intel Q9450 @ 3.2Ghz
Dell 24" 2408WFP | Phillips 37" 1080p
Sapphire HD4870 X2 2GB
4GB Corsair DDR-2 1066 | Thermalrite 120 Ultra Extreme | G9 Mouse | G15 Keyboard
Vista Ultimate x64
pienkie
Registered User
Posts: 1168
Joined: 17 Apr 2007, 02:00
Contact:

Re: GR11 Java Question

Post by pienkie »

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.

Code: Select all

for( ... {
   for( ... {
       for( ... {
       }
    }
}
My dad also says this is the way to go. Thanks.

@Monty - Thanks :)
AMD Tri-Core 720BE @ 3.5Ghz
Biostar TA790GX A3+
4gig OCZ Platinum DDR3-1333
ATI HD4890 @ 1050Mhz Mem / 900Mhz Core

3DMark06: 16 529
Monty
Forum Moderator
Posts: 10000
Joined: 05 Feb 2004, 02:00
Processor: Intel i5-4690K @ 4.5GHZ
Motherboard: ASUS Maximus VII Formula
Graphics card: ASUS GTX970 Strix
Memory: 4 x 4GB Corsair Dominators
Location: Messing with your Mind
Contact:

Re: GR11 Java Question

Post by Monty »

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.
D3PART3D
Starbound's Dear
Posts: 16295
Joined: 01 Dec 2004, 02:00
Contact:

Re: GR11 Java Question

Post by D3PART3D »

That's not fair. We didn't get to learn java at school. :cry:
Ceterum autem censeo Samsung Mobile esse delendam.

When something is important enough, you do it even if the odds are not in your favor.
- Elon Musk
pienkie
Registered User
Posts: 1168
Joined: 17 Apr 2007, 02:00
Contact:

Re: GR11 Java Question

Post by pienkie »

My bad, I was being stupid. When I looked at it just now I snapped for the first time. Here's how it goes now:

*The final version now [The spacing here is terrible; it won't copy and paste properly for some reason]*

Code: Select all

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

3DMark06: 16 529
TheModDoctor
Registered User
Posts: 910
Joined: 15 Oct 2007, 02:00
Location: Hartbeespoortdam

Re: GR11 Java Question

Post by TheModDoctor »

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.
WiK1d
Registered User
Posts: 20732
Joined: 13 Sep 2004, 02:00
Location: Cruising the streets of Pretoria
Contact:

Re: GR11 Java Question

Post by WiK1d »

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.

(Wow, is that sentence even grammatically legal)
Hex_Rated
Registered User
Posts: 3679
Joined: 19 Jan 2006, 02:00
Contact:

Re: GR11 Java Question

Post by Hex_Rated »

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.
DFI LanParty X48 LT-2TR
Intel Q9450 @ 3.2Ghz
Dell 24" 2408WFP | Phillips 37" 1080p
Sapphire HD4870 X2 2GB
4GB Corsair DDR-2 1066 | Thermalrite 120 Ultra Extreme | G9 Mouse | G15 Keyboard
Vista Ultimate x64
RuadRauFlessa
Registered User
Posts: 20576
Joined: 19 Sep 2003, 02:00
Location: Bloodbank

Re: GR11 Java Question

Post by RuadRauFlessa »

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. :lol:
Will post C# code after I tested though.
:rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock:
Spoiler (show)
Intel Core i7-2600k @ 3.4GHz
Corsair Vengence 2x4GB DDR3 2000MHz
Thermaltake Toughpower 850W
ASUS nVidia GTX560 1GB
CoolerMaster HAF 932
RuadRauFlessa
Registered User
Posts: 20576
Joined: 19 Sep 2003, 02:00
Location: Bloodbank

Re: GR11 Java Question

Post by RuadRauFlessa »

BTW please post the execution time for the processing. See if we can hold a match regarding execution times :D

Code: Select all

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();
            }
        }
    }
}
:rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock:
Spoiler (show)
Intel Core i7-2600k @ 3.4GHz
Corsair Vengence 2x4GB DDR3 2000MHz
Thermaltake Toughpower 850W
ASUS nVidia GTX560 1GB
CoolerMaster HAF 932
1gn1t0r
Registered User
Posts: 56
Joined: 07 Feb 2006, 02:00
Contact:

Re: GR11 Java Question

Post by 1gn1t0r »

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
User avatar
Stuart
Lead Forum Administrator
Posts: 38503
Joined: 19 May 2005, 02:00
Location: Home

Re: GR11 Java Question

Post by Stuart »

Please check the date on threads before you reply to something that has probably been solved a long time ago already.
Image
Locked