Login

Welcome, Guest. Please login or register.

February 22, 2026, 01:24:26 am

Author Topic: help  (Read 6701 times)  Share 

0 Members and 1 Guest are viewing this topic.

cara.mel

  • Guest
help
« on: March 06, 2008, 05:07:56 pm »
0
def  myfactorial(n):
   if (n == 0): return 1
   return n * myfactorial(n - 1)

def mysin(x, n):
   if (n == 0): return x
   return ((-1)**n)/myfactorial(2*n + 1) * x**(2*n + 1) + mysin(x, n - 1)


Me trying to translate
def  myfactorial(n):

   if n = 0
then maybe display 1 in a box? idunno

Is this from the else?   return n * myfactorial(n - 1)
then end if here?

def mysin(x, n):
   If n = 0
then again, I dont get what x is doing
   return ((-1)**n)/myfactorial(2*n + 1) * x**(2*n + 1) + mysin(x, n - 1)
^ else? -1^n is just -1 divided by something 2n+1 * x^(2n+1) + something. =/

Its not obvious to follow, I am stupid :(
« Last Edit: March 06, 2008, 06:21:34 pm by caramel »

cara.mel

  • Guest
Re: help
« Reply #1 on: March 06, 2008, 06:18:12 pm »
0
please... :(

i have been told i am supposed to know this in year 11 but i didnt read the first chapter of spec 3/4 because i was told i didnt need to know it.
i will also edit above post to show what I understand

Ahmad

  • Victorian
  • Part of the furniture
  • *****
  • Posts: 1296
  • *dreamy sigh*
  • Respect: +15
Re: help
« Reply #2 on: March 06, 2008, 06:27:29 pm »
0
Well, one way to understand the factorial function defined here is to test it out. Try calculating myfactorial(5).

You'll see that you end up with something like this:
myfactorial(5) = 5*myfactorial(4) = 5*4*myfactorial(3) = 5*4*3*myfactorial(2) = 5*4*3*2*myfactorial(1) = 5*4*3*2*1*myfactorial(0) = 5*4*3*2*1*1 = 5!
Mandark: Please, oh please, set me up on a date with that golden-haired angel who graces our undeserving school with her infinite beauty!

The collage of ideas. The music of reason. The poetry of thought. The canvas of logic.


cara.mel

  • Guest
Re: help
« Reply #3 on: March 06, 2008, 06:29:12 pm »
0
I dont understand that, wouldnt n stay at 5 so youd do 5*5*5*5*5*5*5*5 etc

Neobeo

  • Victorian
  • Trendsetter
  • **
  • Posts: 188
  • 反逆 の Neobeo
  • Respect: +3
Re: help
« Reply #4 on: March 06, 2008, 06:30:27 pm »
0
Basically you just have to note that

And n would not stay at 5 indefinitely since when it recursively calls its own function again, this time it calls with a SMALLER value (in this case 4, then 3, and so on).
Mathematics Coach: Tuition and Enrichment // Email/MSN:

If you remember the flying birds, they died D=

cara.mel

  • Guest
Re: help
« Reply #5 on: March 06, 2008, 06:31:28 pm »
0
write it out in normal code with proper things like ^ and end if in it.

I dont see how n gets smaller, it doesnt say n=n-1 in it

and that doesnt explain the 2nd monster


Edit: I understand what ahmad said but as far as I'm concerned he might have pulled that out of thin air
« Last Edit: March 06, 2008, 06:34:15 pm by caramel »

Ahmad

  • Victorian
  • Part of the furniture
  • *****
  • Posts: 1296
  • *dreamy sigh*
  • Respect: +15
Re: help
« Reply #6 on: March 06, 2008, 06:48:44 pm »
0
def  myfactorial(n):
   if (n == 0): return 1
   return n * myfactorial(n - 1)

Note that the n here is local to the function. So when you call myfactorial(5), n=5, but then you call myfactorial(5-1), which is myfactorial(4), and this creates another framework, where the new n is 4, it can't see the n=5 which is in a 'layer' above it, just as the n inside the myfactorial(5) can't see the n in the myfactorial(4).
Mandark: Please, oh please, set me up on a date with that golden-haired angel who graces our undeserving school with her infinite beauty!

The collage of ideas. The music of reason. The poetry of thought. The canvas of logic.


cara.mel

  • Guest
Re: help
« Reply #7 on: March 06, 2008, 06:51:50 pm »
0
Can you explain what return means

Neobeo

  • Victorian
  • Trendsetter
  • **
  • Posts: 188
  • 反逆 の Neobeo
  • Respect: +3
Re: help
« Reply #8 on: March 06, 2008, 06:53:41 pm »
0
Here goes nothing...

First monster:
def  myfactorial(n):
   if (n == 0):
   {
      return 1
   }
   else:
   {
      return n * myfactorial(n - 1)
   }

Second monster:
def mysin(x, n):
   if (n == 0):
   {
      return x
   }
   else:
   {
      return + mysin(x, n - 1)
   }

Other stuff:
"return" basically terminates the function and goes back to where the function was called, except now the function will have this particular value which was specified by the return
Mathematics Coach: Tuition and Enrichment // Email/MSN:

If you remember the flying birds, they died D=

Ahmad

  • Victorian
  • Part of the furniture
  • *****
  • Posts: 1296
  • *dreamy sigh*
  • Respect: +15
Re: help
« Reply #9 on: March 06, 2008, 06:57:38 pm »
0
Well when programming you usually have two things (to simplify things), functions and routines. Routines are little bits of code which are executed whenever you call the routine.

For example,

routine doPrintStuff()
  print "stuff!"

Now everytime you call doPrintStuff() it will print "stuff". You don't expect doPrintStuff() to "return" a value back.

However, if you had a function such as:

function computeSum(integer a, integer b)
  integer total = a + b
  return total

You expect the function to give back a value. So you can do this, print ( computeSum(5,4) ), and you expect 9 to be printed. You wouldn't, on the other hand, try print ( doPrintStuff() ), because doPrintStuff doesn't "return" a value. I hope you get what return does from the context and this discussion.
Mandark: Please, oh please, set me up on a date with that golden-haired angel who graces our undeserving school with her infinite beauty!

The collage of ideas. The music of reason. The poetry of thought. The canvas of logic.


Ahmad

  • Victorian
  • Part of the furniture
  • *****
  • Posts: 1296
  • *dreamy sigh*
  • Respect: +15
Re: help
« Reply #10 on: March 06, 2008, 06:59:41 pm »
0
And yeah, a little caveat is that once you return a value, execution of the function ceases and the flow of the program immediately returns to the location where the function was called.
Mandark: Please, oh please, set me up on a date with that golden-haired angel who graces our undeserving school with her infinite beauty!

The collage of ideas. The music of reason. The poetry of thought. The canvas of logic.


cara.mel

  • Guest
Re: help
« Reply #11 on: March 06, 2008, 07:01:39 pm »
0
So if it was 5:

5*factorial 4

factorial 4 = 4* factorial 3

factorial 3 = 3* factorial 2

factorial 2 = 2* factorial 1

factorial 1 = 1* factorial 0

factorial 0 = 1

So then factorial 1 = 1*1 = 1
Factorial 2 = 2*1 = 2
Factorial 3 = 3*2 = 6
Factorial 4 = 4*6=24
Facorial 5 = 5*24 = 120

That works

How does it work with 2 numbers in there, x and n. what is x

cara.mel

  • Guest
Re: help
« Reply #12 on: March 06, 2008, 07:02:28 pm »
0
Well when programming you usually have two things (to simplify things), functions and routines. Routines are little bits of code which are executed whenever you call the routine.

For example,

routine doPrintStuff()
  print "stuff!"

Now everytime you call doPrintStuff() it will print "stuff". You don't expect doPrintStuff() to "return" a value back.

However, if you had a function such as:

function computeSum(integer a, integer b)
  integer total = a + b
  return total

You expect the function to give back a value. So you can do this, print ( computeSum(5,4) ), and you expect 9 to be printed. You wouldn't, on the other hand, try print ( doPrintStuff() ), because doPrintStuff doesn't "return" a value. I hope you get what return does from the context and this discussion.

where's it return it to

Ahmad

  • Victorian
  • Part of the furniture
  • *****
  • Posts: 1296
  • *dreamy sigh*
  • Respect: +15
Re: help
« Reply #13 on: March 06, 2008, 07:11:58 pm »
0
Returns it by replacing the function call by the value.

So with print ( computeSum(5,4) ) you'd end up with print ( 9 ). The 9 was returned by the function, so it replaces the function call as a value.
Mandark: Please, oh please, set me up on a date with that golden-haired angel who graces our undeserving school with her infinite beauty!

The collage of ideas. The music of reason. The poetry of thought. The canvas of logic.


Ahmad

  • Victorian
  • Part of the furniture
  • *****
  • Posts: 1296
  • *dreamy sigh*
  • Respect: +15
Re: help
« Reply #14 on: March 06, 2008, 07:13:24 pm »
0
How does it work with 2 numbers in there, x and n. what is x

The x is the argument of the sine function. So you'd be computing sin(x), same x.
Mandark: Please, oh please, set me up on a date with that golden-haired angel who graces our undeserving school with her infinite beauty!

The collage of ideas. The music of reason. The poetry of thought. The canvas of logic.