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: -
'Allocate some local variables
-
Dim LegA As Integer
-
Dim LegB As Integer
-
Dim Hyp As Integer
-
Dim Triple As String
-
Dim Found As Integer
-
-
'Find them
-
For LegA = 1 To 100
-
For LegB = 1 To 100
-
For Hyp = 1 To 200
-
If (LegA ^ 2 + LegB ^ 2 = Hyp ^ 2) Then
-
Found = Found + 1
-
Triple = Found & vbTab & LegA & vbTab & LegB & vbTab & Hyp
-
lstTriples.Items.Add(Triple)
-
End If
-
Next
-
Next
-
Next
-
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?
11 4264
You need to rethink your For loops so that you won't have any duplicates. --Sam
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).
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
Thanks a lot for the help, Sam - I will definitely give it a shot next week when class starts up again.
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.
-
'Find them
-
For LegA = 1 To 100
-
For LegB = 1 To 100
-
For Hyp = 1 To 200
-
If (LegA ^ 2 + LegB ^ 2 = Hyp ^ 2) And LegA < LegB Then
-
Found = Found + 1
-
Triple = Found & vbTab & LegA & vbTab & LegB & vbTab & Hyp
-
lstTriples.Items.Add (Triple)
-
End If
-
Next
-
Next
-
Next
-
Thaaank you.
- 'Find them
-
For LegA = 1 To 100
-
For LegB = 1 To 100
-
For Hyp = 1 To 200
-
If (LegA ^ 2 + LegB ^ 2 = Hyp ^ 2) And LegA < LegB Then
-
Found = Found + 1
-
Triple = Found & vbTab & LegA & vbTab & LegB & vbTab & Hyp
-
lstTriples.Items.Add (Triple)
-
End If
-
Next
-
Next
-
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.
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
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. :)
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)
...(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. :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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;...
|
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...
|
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...
|
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...
|
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:...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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...
| |