Jump to content

What is this function ?


baltoche

Recommended Posts

That capitol Sigma is a summation of the function of P for all values of P between 0 and infinity. Not sure how to solve it though - I haven't done that for years. I'm sure someone else can point you in the right direction - there are some good maths guys here. :)

Link to comment
Share on other sites

Not sure how to solve it though

 

It is an infinite loop.

public static void function(int x){
  int answer = 0;
  int p = 0;
  while(true){
    if(p%2 == 0}{
​       answer = answer + java.lang.Math.pow((x/2),​2p)/java.lang.Math.pow(factorial(p),2)
       system.out.println(answer);
    } else{
       answer = answer - java.lang.Math.pow((x/2),​2p)/java.lang.Math.pow(factorial(p),2)
​​       system.out.println(answer);
    }
  }

}

public static int factorial(int n) {
   int result = 1;
   for (int i = 1; i <= n; i++) {
      result = result * i;
   }
   return result;
}
Link to comment
Share on other sites

You seem to have missed modifying p. And, I'd recommend decreasing the repeated code.

 

Also, the code will "explode" when the ints start overflowing. It won't be infinite!

 

Points for a non-recursive factorial. However, since we know (well, I'm assuming) p increases by 1 each time, we'd be able to keep the last factorial calculated and just multiply by the new p each time, rather than re-calculating all of 1 to p each time. Of course that makes it a non-generic factorial function, more of a "nextvalue" function. (I didn't make this change).

 

(Disclaimer: I didn't read the PDF, I'm just reacting to the code (and I don't "do" java).)

 

 

public static void function(int x){
  int answer = 0;
  int temp = 0;
  int p = 0;
  while(true){
    temp = java.lang.Math.pow((x/2),​2p)/java.lang.Math.pow(factorial(p),2);
    if(p%2 == 0}{
​       answer = answer + temp;
    } else{
       answer = answer - temp;
    }
    system.out.println(answer);
    p++; // p was not changing
  }
}

public static int factorial(int n) {
   int result = 1;
   for (int i = 1; i <= n; i++) {
      result = result * i;
   }
   return result;
}

 

Edited by pzkpfw
Link to comment
Share on other sites

Dear All,

 

Thank you for your respective contributions. I do appreciate ! the reason why I need this function is because I need to integrate it in function of x.

 

Have your codes been typed on Matlab ? Do I need module "Symbolic Math Toolbox" to try your code ?

 

Thanks again and Regards,

Cédric

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.