• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Maths help

Koing

Elite Member <br> Super Moderator<br> Health and F
Help!

I have this:

class Calc
{
static int fun( int i)
{
if (i==0) return 3;
else
return i*i*i+2*i+3+fun(i-1);
}

public static void main(String[] args)
{
System.out.println( "The answer is "+fun(4));
}
/* The answer is 135 */
}

I am asked

b) What is the program Calc doing?
The series I worked out is;

3, 9, 24, 60, 135

I have tried to work out what it does but I don't get it.

Any help is appreciated.

Koing
 
Thanks a lot.

It is probably correct but I can not get it to work out.

example f(2) = 2^3 * 2(2)+f(1)
= 8 * 4 + 9 = 41 The answer should be 24.

Have you left out a +3 from the equation?

Koing
 
Oh, yes. Well, that's the advantage of C++ functions over standard math functions... they can have decision statements.
f(2) = 2^3 * 2*3 + f(1)
f(1) = 1^3 * 1 * 3 + f(0)
f(0) = 3
therefore
f(1) = 1 * 3 + 3 = 3 + 3 = 6
f(2) = 8 * 6 + 6 = 48 + 6 = 54

I hope I didn't make a silly mistake there somewhere.
 
Originally posted by: GigaCluster
Oh, yes. Well, that's the advantage of C++ functions over standard math functions... they can have decision statements.
f(2) = 2^3 * 2*3 + f(1)
f(1) = 1^3 * 1 * 3 + f(0)
f(0) = 3
therefore
f(1) = 1 * 3 + 3 = 3 + 3 = 6
f(2) = 8 * 6 + 6 = 48 + 6 = 54

I hope I didn't make a silly mistake there somewhere.

Jesus H. Christ, you're a genius.
 
Originally posted by: GigaCluster
Oh, yes. Well, that's the advantage of C++ functions over standard math functions... they can have decision statements.
f(2) = 2^3 * 2*3 + f(1)
f(1) = 1^3 * 1 * 3 + f(0)
f(0) = 3
therefore
f(1) = 1 * 3 + 3 = 3 + 3 = 6
f(2) = 8 * 6 + 6 = 48 + 6 = 54

I hope I didn't make a silly mistake there somewhere.

The original code is this basically

if (i==0) return 3;
else
return i*i*i+2*i+3+fun(i-1);

Yours is
f(n)=n^3*2n+f(n-1)

The series I get from the code is this

f(0) = 3
f(1) = 9
f(2) = 24
f(3) = 60
f(4) = 135

How did you get the extra 3 in your working out?

f(2) = 2^3 * 2*3 + f(1)
f(1) = 1^3 * 1 * 3 + f(0)
f(0) = 3
therefore
f(1) = 1 * 3 + 3 = 3 + 3 = 6
f(2) = 8 * 6 + 6 = 48 + 6 = 54

From this formula?
f(n)=n^3*2n+f(n-1)

I follow the n^3 part.

The "*2n" part you don't seem to have in your examples?

The last part "+f(n-1) you have and I get that.

Koing
 
f(0) = 3
f(1) = 9 = f(0) + (1)^3 + 2(1) + 3
f(2) = 24 = f(1) + (2)^3 + 2(2) + 3
f(3) = 60 = f(2) + (3)^3 + 2(3) + 3
f(4) = 135 = f(3) + (4)^3 + 2(4) + 3

f(n) = f(n-1) + (n)^3 + 2(n) + 3

He messed it up slightly.
 
Pfft... I did make a mistake. I omitted the "+3+".
Instead of: f(n)=n^3*2n+f(n-1)
It should be: f(n)=n^3*2n+3+f(n-1) unless n is zero.

So, my calculations are wrong as well. Let's see... second try:

f(2) = 2^3 * 2*3 + 3 + f(1)
f(1) = 1^3 * 1*3 + 3 + f(0)
f(0) = 3
therefore
f(1) = 1 * 3 + 3 + 3 = 3 + 3 + 3 = 9
f(2) = 8 * 6 + 3 + 9 = 48 + 3 + 9 = 60

Sorry for all the confusion... I will kick myself if this is also wrong.
 
Originally posted by: GigaCluster
Pfft... I did make a mistake. I omitted the "+3+".
Instead of: f(n)=n^3*2n+f(n-1)
It should be: f(n)=n^3*2n+3+f(n-1) unless n is zero.

So, my calculations are wrong as well. Let's see... second try:

f(2) = 2^3 * 2*3 + 3 + f(1)
f(1) = 1^3 * 1*3 + 3 + f(0)
f(0) = 3
therefore
f(1) = 1 * 3 + 3 + 3 = 3 + 3 + 3 = 9
f(2) = 8 * 6 + 3 + 9 = 48 + 3 + 9 = 60

Sorry for all the confusion... I will kick myself if this is also wrong.

Ahhhhh no biggie at least I see I'm not going mad and not getting it.

Thank you VERY much for the help and everybody else that contributed.

😀

Koing

 
You messed up again...don't kick yourself, just get some sleep.

It is:
f(n) = f(n-1) + (n)^3 + 2(n) + 3
NOT
f(n) = n^3 *2n + 3+ f(n-1)
 
It's OK. I'm currently a math major, and you're probably tired. I am curious as to why Koing is asking for homework help in the early morning hours of Saturday (he lives in Great Britain).
 
Originally posted by: crt1530
It's OK. I'm currently a math major, and you're probably tired. I am curious as to why Koing is asking for homework help in the early morning hours of Saturday (he lives in Great Britain).

Thanks for ALL your help everybody.

I was doing it about 4am or so or whatever as I was ready to go to sleep (Saturday no training as this beer festival is on and the gym is closed today = wake up late 😀) then a friend messaged me. Had a few problems and such about the work and I decided to have a look. Got interested and wanted to finish this part off.

How do you guys in the states call "Maths" help then? The formal way would be Mathematics. Mathes with an e is not correct in UK.

Was wondering why I needed English help, as my post looked alright as far as things went I thought.

Koing
 
Originally posted by: Koing


How do you guys in the states call "Maths" help then? The formal way would be Mathematics. Mathes with an e is not correct in UK.

Was wondering why I needed English help, as my post looked alright as far as things went I thought.

Koing


Math help. And it's Stat help for Statistics help.
 
Originally posted by: OulOat
Originally posted by: Koing


How do you guys in the states call "Maths" help then? The formal way would be Mathematics. Mathes with an e is not correct in UK.

Was wondering why I needed English help, as my post looked alright as far as things went I thought.

Koing


Math help. And it's Stat help for Statistics help.


So you don't say the s then? Hmm doesn't sound right to me.

I guess being in England too long has this affect on a person. Much like your use of "z" for the "ized" types of words and English use "ised" mainly. Same with colour and color etc.

Koing
 
Back
Top