none
Factorial calculations using loops

    Question

  • Trying to figure out how to calculate factorials via loops. Cant seem to figure out where to start when using loops. Any help would be appreciated.

     

    Dan

    Saturday, July 17, 2010 2:43 AM

Answers


  •     intAnswer = intCounter
        Do
          intCounter = intCounter -1
          intAnswer = intAnswer * intCounter
        Loop Until intCounter = 1
      End Sub

    • Marked as answer by Drakozs Saturday, July 17, 2010 6:24 AM
    Saturday, July 17, 2010 5:52 AM

All replies


  • You will need to check for overflows, negative parameters, but in essence:
     
        Function factorial(ByVal x As Long) As Long
            Dim b As Long = 1
            For a As Long = 2 To x
                b = b * a
            Next
            Return b
        End Function

    --
    Mike
    Saturday, July 17, 2010 2:55 AM
  • How does that work in a loop? Key thing im looking for is in a loop. Sorry but thanks though.
    Saturday, July 17, 2010 3:34 AM
  • Trying to figure out how to calculate factorials via loops. Cant seem to figure out where to start when using loops. Any help would be appreciated.

     

    Dan

    Hi Drakozs,

    Please see these two threads.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4ec2bc19-b35d-4e27-9790-2bdf4b5ff153

     

    and.>>

     

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bb23b85f-9a88-4dae-a54e-df71cd25b59d/

     


    Regards, John
    Saturday, July 17, 2010 3:36 AM
  • Thanks bud, I didnt see that second link when I searched. Not im just trying to find a way to fit that to work with my project im working on.

     

    Dan

    Saturday, July 17, 2010 3:57 AM
  • The problem im having with is using the examples you linked to gives 500 as a default answer, Im trying to create one that lets the user input a number 1-10 to factorial.

     

    Edited.

     

    Going to post what I'm going with at the moment, and see if I'm going the right direction. The question I'm trying to figure out is, I have intAnswer that gave me the first part of the factorial, but if that is going to loop each time how do i keep my answer adding up. I appreciate the help.

     

    Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCompute.Click
        Dim intCounter As Integer
        Dim intSum As Integer
        Dim intAnswer As Integer
    
        intCounter = Val(InputBox("Enter a Value between 1-10)", "User Input"))
        Do
          Application.DoEvents()
          intSum = intCounter
          intAnswer = (intSum * (intSum - 1))
          intCounter = intCounter - 1
        Loop Until intCounter = 0
      End Sub

    Saturday, July 17, 2010 4:02 AM

  •     intAnswer = intCounter
        Do
          intCounter = intCounter -1
          intAnswer = intAnswer * intCounter
        Loop Until intCounter = 1
      End Sub

    • Marked as answer by Drakozs Saturday, July 17, 2010 6:24 AM
    Saturday, July 17, 2010 5:52 AM
  • Thanks bud, thats right what i needed. Now i feel retarded.
    Saturday, July 17, 2010 6:25 AM

  • How is "for a as long = 2 to x" not a loop???

    --
    Mike
    Saturday, July 17, 2010 12:24 PM
  • In this reference http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4ec2bc19-b35d-4e27-9790-2bdf4b5ff153 I used .Net 4.0 BigInteger to calculate the factorial.  If you use integer or long, you can not calculate very many before you get an overflow.

    Subnet Calculator / Planner      Serial Port
    Saturday, July 17, 2010 6:34 PM