none
recursion RRS feed

  • Question

  • def quuR (quuN) :
        
        if quuN == 1 :
            
            return 1  
        
        else : 
            
            return ( quuN * quuR(quuN - 1))
       
    quuNum = int(input('ENTER NUMBER'))
    
    if quuNum >= 1 :
        
        print( "The factorial of ", quuNum , "is", quuR(quuNum))
    Why do I have to include the " if quuNum >= 1" section? 

    iV


    • Edited by iivii Tuesday, July 12, 2016 7:26 PM
    Tuesday, July 12, 2016 7:25 PM

Answers

  • Why do I have to include the " if quuNum >= 1" section? 

    * To ignore input resp. calculation for input values < 1, where NO n! is defined. Eg. try in MSCalc, input eg. -1 and try n! -> Invalid input! (or similar in engl.)

    Tuesday, July 12, 2016 10:39 PM
    Answerer

All replies

  • Hi !

    Here, you are on the Small Basic forum, all about BASIC language only.

    If you are using Visual C#, Visual Studio language i would recommend this forum:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=vslanguages

    If you use Java, Java script ,  or any other languages, i would recommended you to find the best forum to suit your needs :)


    Tuesday, July 12, 2016 8:41 PM
    Answerer
  • Factorial Demo  in Small Basic:

    ' Factorial by recursive func
    n=0   '  n!
    i=0   ' Index Factorial
    in=0  ' Input n
    TextWindow.BackgroundColor = "Black"
    TextWindow.Clear()
    
    While in >= 0
     TextWindow.ForegroundColor = "Red"
     TextWindow.Write("Enter number < 28 to calc n!:  ")
     TextWindow.ForegroundColor = "Green"
     in = Math.Floor(  TextWindow.ReadNumber()  )
     if in >= 0 then
       i = in
       n = i
       fac()
       TextWindow.ForegroundColor = "yellow"
       TextWindow.WriteLine("                   Factorial:  " + n)
     endif
    endwhile
    Program.End()
    
    Sub fac
      i=i-1
      If in <= 0 Then 
        n=1
      elseIf i = 0 Then 
        'TextWindow.WriteLine("end in:"+ in + "n:"+ n + "i:"+ i)
      Else
        n=i*n
        'TextWindow.WriteLine("calc in:"+ in + "n:"+ n + "i:"+ i)
        fac()
       endif
    EndSub
    The break condition is  inside  of the recursive function and goes directly to return or endsub.

    Tuesday, July 12, 2016 9:56 PM
  • Why do I have to include the " if quuNum >= 1" section? 

    * To ignore input resp. calculation for input values < 1, where NO n! is defined. Eg. try in MSCalc, input eg. -1 and try n! -> Invalid input! (or similar in engl.)

    Tuesday, July 12, 2016 10:39 PM
    Answerer
  • Attention!

    0! = 1

    1! = 1

    2! = 2

    3! = 6

    ...

    Tuesday, July 12, 2016 11:19 PM
  • Yes, the 0! = 1  konvention, but the sample at the top only uses integers >= 1.
    Tuesday, July 12, 2016 11:41 PM
    Answerer