Results 1 to 4 of 4

Math Help - python questions...please help!!

  1. #1
    Newbie
    Joined
    Dec 2007
    From
    UK
    Posts
    23

    Question python questions...please help!!

    hi,

    could someone please help me with the attached python (repetition:Iteration) questions....

    any help will be greatly appreciated!!

    *

    Attached Files Attached Files
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Junior Member
    Joined
    Feb 2006
    From
    United Kingdom
    Posts
    70
    1.)
    consider the function

    Code:
    def infinite_recursion():
       return infinite_recursion()
    if we write this infinite recursive function then it gives the error:

    Code:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in infinite_recursion
      ...
    with the final line repeating over and over.

    if we just have an infinitely repeating loop. ie:

    Code:
    i=0
    while 1: i+=1
    we never get an error, and it just keeps repeating the loop, doing as its told.

    2.)
    recursive function to generate \sum_{i=1}^n i

    Code:
    def n_sum(n):
       if n <= 1: return 1 #base case
       return n+n_sum(n-1) #inductive step
    for fibonacci sequence:

    Code:
    def nth_fib(n):
       if n <= 1: return 1                #covers our base cases
       return nth_fib(n-1) + nth_fib(n-2) #inductive step
    for fib(0) and fib(1) we call our function once only (as it is covered in the base case)

    for fib(n) we call our function how many times it calls for both n and n-1
    so calls(n) = calls(n-1) + calls(n-2)
    this is the fibonacci relation, and we have the same initial cases:
    calls(0) = calls(1) = 1
    so calls(n) = fib(n)

    ie the function is called 8 times with fib(5).

    3.)
    Code:
    i = 0
    while i < 100:
       i+=1
       if i%3 == 2:
          print i, "mod", 3, "= 2"

    Code:
    for i in range(0,100):
       if i%2 == 0:
          print i, "is even"
       else:
          print i, "is odd"
    4.
    it will execute zero times.
    this is because it executes once for each value i takes in the list []. this is an empty list so i takes no values.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Dec 2007
    From
    UK
    Posts
    23
    Quote Originally Posted by Aradesh View Post
    1.)
    consider the function

    Code:
    def infinite_recursion():
       return infinite_recursion()
    if we write this infinite recursive function then it gives the error:

    Code:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in infinite_recursion
      ...
    with the final line repeating over and over.

    if we just have an infinitely repeating loop. ie:

    Code:
    i=0
    while 1: i+=1
    we never get an error, and it just keeps repeating the loop, doing as its told.

    2.)
    recursive function to generate \sum_{i=1}^n i

    Code:
    def n_sum(n):
       if n <= 1: return 1 #base case
       return n+n_sum(n-1) #inductive step
    for fibonacci sequence:

    Code:
    def nth_fib(n):
       if n <= 1: return 1                #covers our base cases
       return nth_fib(n-1) + nth_fib(n-2) #inductive step
    for fib(0) and fib(1) we call our function once only (as it is covered in the base case)

    for fib(n) we call our function how many times it calls for both n and n-1
    so calls(n) = calls(n-1) + calls(n-2)
    this is the fibonacci relation, and we have the same initial cases:
    calls(0) = calls(1) = 1
    so calls(n) = fib(n)

    ie the function is called 8 times with fib(5).
    hi,
    thanks for the help!

    i tried but i cant seem to get the infinite recursion or fibbonaci sequence to work?!?
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Junior Member
    Joined
    Feb 2006
    From
    United Kingdom
    Posts
    70
    really? the fibonacci one should work fine (it does for me)... just paste it in, and to call it put \text{nth\_fib(5)}, for example, it should return 8 in that case.

    the infinite recursion one isn't really meant to work... its just a function that should infinitely call itself, and this seems to cause errors (and the question asks what error do you get, so i'm guessing it should.)
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Python nth prime
    Posted in the Math Software Forum
    Replies: 1
    Last Post: August 17th 2011, 12:06 AM
  2. Invalid syntax in Python
    Posted in the Math Software Forum
    Replies: 7
    Last Post: October 24th 2010, 06:29 PM
  3. Python-conditions!!!
    Posted in the Math Software Forum
    Replies: 1
    Last Post: December 8th 2007, 06:27 AM
  4. Python - Repetition (Iteration)
    Posted in the Math Software Forum
    Replies: 3
    Last Post: December 8th 2007, 05:36 AM
  5. Python questions...Help!!!(Basics/Boolean/Choice)
    Posted in the Math Software Forum
    Replies: 2
    Last Post: December 7th 2007, 04:43 AM

Search Tags


/mathhelpforum @mathhelpforum