View Single Post
Old 17-08-2009, 20:27   #76
Fobic
Inactive
 
Join Date: Dec 2005
Posts: 23
Fobic is a jewel in the roughFobic is a jewel in the roughFobic is a jewel in the roughFobic is a jewel in the roughFobic is a jewel in the rough
Re: Programming Challenges?

Before we move on too far here's my attempts at the previous challenges
Both are VB.net console Apps.

Mutliples of 3 and 5 without using any type of Modulus functions:

Spoiler: 

Code:
Module MultiplesOf3And5
 
   Sub Main()
      Dim aList As New SortedList
 
      '//iterate through all the possible multiples
      '//and store any previously unfound ones in a Sorted List
      For intFifteens As Integer = 3 * 5 To 100 Step 3 * 5
         aList.Add(intFifteens, " is a multiple of both 3 and 5")
      Next intFifteens
 
      For intFives As Integer = 5 To 100 Step 5
         If Not aList.Contains(intFives) Then aList.Add(intFives, " is a multiple of 5")
      Next intFives
 
      For intThrees As Integer = 3 To 100 Step 3
         If Not aList.Contains(intThrees) Then aList.Add(intThrees, " is a multiple of 3")
      Next intThrees
 
      '//Display the contents of the Sorted List
      For intIndex As Integer = 0 To aList.Count - 1
         Console.WriteLine(aList.GetKey(intIndex) & aList.GetByIndex(intIndex))
      Next intIndex
      Console.ReadKey(True)
 
   End Sub
 
End Module
The For/Next Loops step through each multiple
of 3*5, 5 and 3 and adds the multiples to a Sorted List
if they haven't already been added.

A Sorted List is a list where each item in the list
consists of two parts: a Key part and a Value part.
The list is automatically sorted on the Key part.

We store each iterated multiple in the the Key part
and text explaining the type of multiple is
stored in the corresponding Value part.

Then we just display the contents of the Sorted List.



And a quick and dirty way of calculating the Monty Hall probabilities
(with thanks to Damien for coding tips)

Spoiler: 

Code:
Module MontyHall
 
   Sub Main()
      Dim aRnd As New Random
      Dim Doors() As String = {"Goat", "Goat", "Goat"}   '//put 3 Goats behind 3 doors
 
      Dim intDoorWithCar As Integer                '//Number of the Door with the Car
      Dim intDoorChosen As Integer                 '//Number of the Door we choose
      Dim intChosenDoorWinCount As Integer = 0     '//Accumulated wins sticking with original chosen door
      Dim intAlternateDoorWinCount As Integer = 0  '//Accumulated wins swapping to alternate door
      Dim intNumIterations As Integer = 100000
 
      For intI As Integer = 1 To intNumIterations
         intDoorWithCar = aRnd.Next(3)    '//Randomly replace one of the 
         Doors(intDoorWithCar) = "Car"    '//   3 Goats with a Car
 
         intDoorChosen = aRnd.Next(3)     '//Choose one of the 3 doors at random
         If intDoorChosen = intDoorWithCar Then intChosenDoorWinCount += 1 Else intAlternateDoorWinCount += 1
 
         Doors(intDoorWithCar) = "Goat"   '//Replace the Car with a Goat
      Next intI                           '// and go around again
 
      Console.WriteLine("Percentage probability of winning Car:")
      Console.WriteLine("Keeping Original Choice of Door   = " & Format(intChosenDoorWinCount / intNumIterations, "Percent"))
      Console.WriteLine("Swapping Choice to the Other Door = " & Format(intAlternateDoorWinCount / intNumIterations, "Percent"))
      Console.ReadKey(True)
 
   End Sub
 
End Module

We choose one of the 3 doors at random.
Monty removes one of the remaining 2 doors
that has a goat behind it.
We don't need to know which door, just that a goat
has been removed and now we only have 2 doors left
- A goat behind one and a Car behind the other, and
that our chosen door is one of them.

The If/THEN/ELSE line of the code works as follows:

If we stick with our choice and win, then
increase the count of wins for the Chosen Door.
(in the "Swapping Doors Scenario" we would have lost)

If we stick with our choice and lose, then in the
"Swapping Doors Scenario" we would have won, so
increase the count of wins for the Alternate Door.

Fobic is offline   Reply With Quote