Parse poisson function in javascript to php [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the next code in javascript:
var exponential = 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746;
var numerator, denominator;
function fact(x) {
if(x==0) {
return 1;
}
return x * fact(x-1);
}
function poisson(k, landa) {
exponentialPower = Math.pow(exponential, -landa); // negative power k
landaPowerK = Math.pow(landa, k); // Landa elevated k
numerator = exponentialPower * landaPowerK;
denominator = fact(k); // factorial of k.
return (numerator / denominator);
}
I need parse to php but i don't know how...
Can somebody help me?

you should put below variables within poisson function also dont think you need to initialize $numerator and $dominator to 0;
$exponential = 2;
$numerator = 0;
$dominator = 0;
function fact($x) {
if($x==0) {
return 1;
}
return $x * fact($x-1);
}
function poisson($k, $landa)
{
$exponential = 2;
$exponentialPower = pow($exponential, -$landa);
$landaPowerK = pow($landa,$k);
$numerator = $exponentialPower * $landaPowerK;
$dominator = fact($k);
echo ($numerator / $dominator);
}
poisson(1,2);

Related

How to refactor this code with if statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have code with if statement (below). Is it possible to refactor this code? because in this method is ugly if statement :(
getOverallAmount(estimate) {
const {installments} = estimate;
const {vipValue} = estimate;
if (installments !== 1) {
const oneInstallment = vipValue / installments;
const allWithOneInst = vipValue - (oneInstallment * installments) + 1;
return oneInstallment + allWithOneInst;
}
return vipValue;
}
Using a ternary operator instead of the if statement:
getOverallAmount(estimate) {
const {installments} = estimate;
const {vipValue} = estimate;
const oneInstallment = vipValue / installments;
const allWithOneInst = vipValue - (oneInstallment * installments) + 1;
return (installments !== 1) ? oneInstallment + allWithOneInst : vipValue;
}

Debugging small javascript with exponents [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
n = 1;
while (n < 1024) {
alert(Math.pow(n, 2));
n = n + 2;
}
How can I make this display the powers of 2 from 2^0 to 2^10 inclusive i.e. (1-1024) and end once it reaches 1024 in Javascript
I would do something like this:
for(let n = 0; n <= 10; n++) {
console.log(2 ** n)
}
This logs the answer of each loop iteration. As Pointy suggested in the comment, we have to make the n value iterate from 1 to 10, since that is the variable that generates the output, whereas the 1024 is simply the output that comes from the power values. You could also use Math.pow as follows:
for(let n = 0; n <= 10; n++) {
console.log(Math.pow(2, n))
}
Implementing Secan's suggestion, here is a function which will take any number as a parameter:
function foo(x, exp) {
for(let i = 0; i <= exp; i++) {
console.log(x ** i)
}
}
So to apply this to the original answer, we would call it like this:
foo(2, 10)
Sorry for the ambuiguity with my original question. I've figured it out-
n=0;
while (n <= 10) {
alert(Math.pow(2, n));
n = n + 1;
}
Setting n=0, n<=10 in while and changing n + 1 instead of 2 resolved my issue. Thanks for your responses!

Mathematics functions by string, JS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm looking for a function that return me a mathematic function following those examples:
The_Function_I_Need("x^2") = function(x) { return math.pow(x, 2) }
The_Function_I_Need("x*2+5") = function(x) { return 2*x+5 }
The_Function_I_Need("x+y") = function(x, y) { return x+y }
And more..
var The_Function_I_Need = {
"x^2": function(x) {
return Math.pow(x, 2);
},
"x*2+5": function(x) {
return 2 * x + 5;
},
"x+y": function(x, y) {
return x + y;
}
}
console.log(The_Function_I_Need["x^2"](4))
console.log(The_Function_I_Need["x*2+5"](4))
console.log(The_Function_I_Need["x+y"](2, 4))
The Javascript eval() function can evaluate a string as an expression.
To follow your example, you could just use eval("x^2").
Reference: http://www.w3schools.com/jsref/jsref_eval.asp

How to add and remove to 2 variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
im trying to add and remove to 2 different variables in a code for a incremental game.
here is the code:
$("#autoPayCheckBuy").click(function () {
if (totalClicks >= 50) {
totalClicks -= 50;
totalMoneys += 50;
$("#total_moneys").text(totalMoneys);
$("#total_clicks").text(totalClicks);
}
});
and here is the code for the money/clicks:
var totalClicks = 0;
var totalMoneys = 100;
sorry that i cant give a exact problem, i just cant find a problem in the code :(
I can't find a problem in the code either. What exactly is your problem? Maybe you are missing the d.ready ?
var totalClicks = 0;
var totalMoneys = 100;
$( document ).ready(function() {
$("#autoPayCheckBuy").click(function () {
if (totalClicks < 50) return false;
totalClicks -= 50;
totalMoneys += 50;
$("#total_moneys").text(totalMoneys);
$("#total_clicks").text(totalClicks);
});
});

Repeat function takin previous result of this function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
var x = function (a) { return a + a/4 - 600}
I have a function that does something.
So now my goal is to repeat this function 12 times with an argument used from previous operation;
Let's say initial ammount is 5000;
So
`x(5000) = 5650;
x(5650) =6462.5;
x(6462.) =...;
and this should be repeated 12 times;
So how can this be done in code?
`
Certainly the easiest way would be to use a for loop:
var x = function(a) { return a + a/4 - 600 },
v = 5000;
for (var i = 0; i < 12; i++) {
v = x(v);
}
console.log(v);
I'd not recommend doing something like this, but instead of a loop you could use recursion:
var x = function(42.34, 12);
----
function(double a, int maxSize)
{
a = ((a + a) / (4 - 600));
if(maxSize>0){
maxSize--;
return function(a,maxSize);
}else{
return a;
}
}
(just pseudocode)

Categories

Resources