2

Sorry I couldn't think of a good title.

I am working on a ICT related exercise and come across this:

Calculate alg a(n) and alg b(n) for n = 1,2,3,4 and 5

(a)
    alg_a(n):result
    if n > 1 then
    return(alg_a(n−1)+alg_a(n−1))
    else return(1)

(b)
    alg_b(n):result
    if n > 1 then
    return(2 · alg_b(n−1))
    else return(1)

At first, what does the code at line 1 do (alg_a(n):result)?

A: The question asks me to calculate alg a(n) so lets say I insert 1, if n > 1 --> no --> return 1. But what happens when I insert n = 2.

Any help is appreciated,

thanks!

3
  • What (programming) language is this? Please add that tag to the question =) Commented Jan 12, 2012 at 13:24
  • Since these functions are recursive, once you have a solution for n = 1 then you can use this to calculate the result for n = 2. And so on, for n = 3, 4, 5... Commented Jan 12, 2012 at 13:26
  • I have absolutely no idea which programming language this is. Most questions are written in pseudocode. Commented Jan 12, 2012 at 13:30

2 Answers 2

6

algorithm alg_a(n) calculates 2^(n-1) and alg_b(n) does the same thing.
Theese are recursive functions. For example for 4 alg_a returns:
alg_a(4)=
alg_a(3) + alg_a(3) =
alg_a(2) + alg_a(2) + alg_a(2) + alg_a(2) =
alg_a(1) + alg_a(1) + alg_a(1) + alg_a(1) + alg_a(1) + alg_a(1) + alg_a(1) + alg_a(1) = 8

Sign up to request clarification or add additional context in comments.

Comments

3

This isn't code, it's some form of pseudo-code. The work "result" just means that what follows is the result of the function. So, alg_a(1) gives you the result of 1, whereas alg_a(2) gives you the result (alg_a(1) + alg_a(1)), i.e. 2. Continue to get your other answers.

The question in this case isn't asking for anything more complicated than the numeric answers.

1 Comment

1 = 1 2 = 1*1+1*1 = 2 3 = 2*2+2*2 = 4 4 = 4*3+4*3 = 24 5 = 24*4+24*4 = 192 Is this correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.