473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pythagorean triples

I am in a VB .NET class, and one of the assignments is to use loops to generate a list of all the pythagorean triples where legA and legB <100 and hypotenuse < 200, and then put this list into a listbox.
I have done this so far:

Expand|Select|Wrap|Line Numbers
  1.  'Allocate some local variables
  2.         Dim LegA As Integer
  3.         Dim LegB As Integer
  4.         Dim Hyp As Integer
  5.         Dim Triple As String
  6.         Dim Found As Integer
  7.  
  8.         'Find them
  9.         For LegA = 1 To 100
  10.             For LegB = 1 To 100
  11.                 For Hyp = 1 To 200
  12.                     If (LegA ^ 2 + LegB ^ 2 = Hyp ^ 2) Then
  13.                         Found = Found + 1
  14.                         Triple = Found & vbTab & LegA & vbTab & LegB & vbTab & Hyp
  15.                         lstTriples.Items.Add(Triple)
  16.                     End If
  17.                 Next
  18.             Next
  19.         Next
  20.  
but this generates duplicate triples (3,4,5 and 4,3,5), which the instructor does not want.
I've gone through everything I can think of, and looked at all the different listbox functions and help topics, but I'm at a loss.
Any ideas on how to eliminate the duplicate triples that appear in the listbox?
Mar 9 '07 #1
11 4264
SammyB
807 Expert 512MB
You need to rethink your For loops so that you won't have any duplicates. --Sam
Mar 9 '07 #2
You need to rethink your For loops so that you won't have any duplicates. --Sam
Clearly, but could you point me in the direction of something somewhat more substantial? I have (as I mentioned in passing) been racking my brain trying to come up with a solution, but am at a loss (normally these things come pretty quickly).

Thanks for the reply and any future help (that, of course, falls within the limits of morality).
Mar 9 '07 #3
SammyB
807 Expert 512MB
Clearly, but could you point me in the direction of something somewhat more substantial? I have (as I mentioned in passing) been racking my brain trying to come up with a solution, but am at a loss (normally these things come pretty quickly).

Thanks for the reply and any future help (that, of course, falls within the limits of morality).
As you picked up, I'm trying (as a teacher myself) to make you think of how to solve the problem. A couple more ideas: (1) Do you really need three loops? From trig, you know that the hyp squared = the sum of the squares of the other two sides. (2)Reduce you side limits to something small and write out the variables of the For loop, so that you can see how to make the iterations only cove the possibilities once; for example, when the LeqA For loop is on 2, is there any reason to start the LegB loop at 1? Haven't you already covered that case? Keep smiling! --Sam
Mar 10 '07 #4
Thanks a lot for the help, Sam - I will definitely give it a shot next week when class starts up again.
Mar 12 '07 #5
SammyB
807 Expert 512MB
Thanks a lot for the help, Sam - I will definitely give it a shot next week when class starts up again.
¿Next week? Don’t procrastinate! Do it now. ;o)>>>

Another good exercise for you: if you write out your loops with some small ending values, so that you can easily go through several iterations, then you should be able to find another (poor, but interesting) solution where you leave your For loops alone and just add a single if statement. Keep smiling, Sam.
Mar 13 '07 #6
Expand|Select|Wrap|Line Numbers
  1. 'Find them
  2. For LegA = 1 To 100
  3.    For LegB = 1 To 100
  4.       For Hyp = 1 To 200
  5.          If (LegA ^ 2 + LegB ^ 2 = Hyp ^ 2) And LegA < LegB Then
  6.             Found = Found + 1
  7.             Triple = Found & vbTab & LegA & vbTab & LegB & vbTab & Hyp
  8.             lstTriples.Items.Add (Triple)
  9.          End If
  10.       Next
  11.    Next
  12. Next
  13.  
Thaaank you.
Mar 19 '07 #7
Killer42
8,435 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. 'Find them
  2. For LegA = 1 To 100
  3.   For LegB = 1 To 100
  4.     For Hyp = 1 To 200
  5.       If (LegA ^ 2 + LegB ^ 2 = Hyp ^ 2) And LegA < LegB Then
  6.         Found = Found + 1
  7.         Triple = Found & vbTab & LegA & vbTab & LegB & vbTab & Hyp
  8.         lstTriples.Items.Add (Triple)
  9.       End If
  10.     Next
  11.   Next
  12. Next
Um... Isn't that IF statement kind of pointless? Why check all the lower values of B at all, when you can just start the B loop with a higher value.

Also, I don't understand why the third loop even exists. I though all you needed to know about Hyp is that it's less than 200. So why not just calculate it from the other sides, and test it? Seems to me, this program does more than 200 times as much work as it needs to.
Mar 19 '07 #8
SammyB
807 Expert 512MB
As Killer says, although your solution works, but it shows that you still don't understand loops. Look back at post #4 in this thread and use it along with Killer's suggestion to come up with a much better solution. --Sam
Mar 19 '07 #9
Killer42
8,435 Expert 8TB
As Killer says, although your solution works, but it shows that you still don't understand loops. Look back at post #4 in this thread and use it along with Killer's suggestion to come up with a much better solution. --Sam
Thanks Sam. As you can see, I resisted the temptation to just code it myself and post it. Though not a teacher, I do generally prefer to try and lead the poster to understand the question, if possible. :)
Mar 19 '07 #10
I'm with you on both points -
But I got yelled at when I changed the loop for B to anything but 1 to 100 =/

So quick fix, whatever, as opposed to something like For A = 1 to 100, For B = A to 100

EDIT:
(My teacher loves to talk about the "2 Million Combinations! Wow!" this program goes through)
Mar 22 '07 #11
Killer42
8,435 Expert 8TB
...(My teacher loves to talk about the "2 Million Combinations! Wow!" this program goes through)
Let's just hope the teacher doesn't ask you to check them by hand. :)
Mar 23 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

37
by: Jason Heyes | last post by:
A pythagorean triple is a triple <a,b,c> whose components are positive integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 + 4*4 = 9 + 16 = 25 = 5*5. I want to write a function...
6
by: 3than7 | last post by:
I am writing an application to solve Pythagorean Theorum Problems. This is on my own time, i am using a book to learn c++, and after doing a fahrenheit to celsuis program from that book, i wanted...
3
by: Leeh | last post by:
I'm new to the world of RDF and RDF/XML so pardon my naive question: I understand that the "real" RDF model is the conceptual network of nodes (Subjects and Objects) connected by predicate arcs;...
1
by: painkiller4nobrain | last post by:
I'm in VB.NET class. teacher gave assignment abt Pythagorean triples. but i'm lost. i don't kno wat he is tryin to said. I'm confused with all the codes tht VB.NET. We are using 2003 version n this...
5
by: stephanieanne2 | last post by:
The Problem: A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy...
11
by: inferi9 | last post by:
hi everyone I am new here and I have this C++ program that I have to write but it keep given me nothing useful. here is the question: A right triangle can have sides that are all integers. A...
12
by: abkierstein | last post by:
This is my 1st program and I need some help. I've almost got this one finished but I don't know where to go from here. There is something wrong with the sides I've assigned. Any tips? // Program:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.