473,837 Members | 1,545 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 4308
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
3418
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
2038
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
1677
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
1135
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
4719
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
15069
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
7430
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
9840
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
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10565
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10623
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,...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5847
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
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
3124
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.