473,699 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pythagorean triples

5 New Member
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 4292
SammyB
807 Recognized Expert Contributor
You need to rethink your For loops so that you won't have any duplicates. --Sam
Mar 9 '07 #2
corwood
5 New Member
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 Recognized Expert Contributor
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
corwood
5 New Member
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 Recognized Expert Contributor
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
corwood
5 New Member
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 Recognized Expert Expert
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 Recognized Expert Contributor
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 Recognized Expert Expert
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

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

Similar topics

37
3393
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 to extract pythagorean triples from an input stream. The input is formatted so that only the first two components <a,b> of a pythagorean triple are specified. The function signature will be: std::istream &operator>>(std::istream &is,...
6
2031
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 to try to make something all be meself. I have it working great to find the hypotenuse, but am having some dufficulty making it produce a missing leg. As you know, a^2 + b^2 = c^2 I have a variable that does the input for the one of the legs...
3
1669
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; and that the official way to serialize the graph is to use the RDF/XML specification. So far so good; but N3 and/or N-Triple notations are also used, and it sure seems to me that N3 is "iso-morphic" to the graph; i.e. The triples (properly...
1
1123
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 is wat teacher askin me to do. question.bmp n this is wat i hav done this so far
5
4711
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 the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. Find all Pythagorean triples for side1, side2, and the hypotenuse that fall within a user-specified range. Limit the upper-bound to 500. Use a...
11
15051
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 set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the following relationship: (side1)^2 + (side2)^2 = (hypotenuse)^2 Output all Pythagorean...
12
7417
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: Pythagorean Theorem // Written by: Hellbreaker // // #include <iostream> #include <cmath> using namespace std;
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9197
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8941
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8897
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.