Euler
Download scriptThis script is designed to perform a specific mathematical calculation to derive a constant value known as Euler's number (often represented as e). Instead of simply providing a pre-set number, the code dynamically calculates this value using a series of mathematical operations.
How it Works
The logic follows a two-step process to reach the final result:
1. The Factorial Calculation To begin the process, the script includes a tool to calculate "factorials." In mathematics, a factorial is the product of an integer and all the integers below it down to one. For example, the factorial of 4 is $4 \times 3 \times 2 \times 1$. The script uses a dedicated function to handle these multiplications automatically, ensuring that as the numbers get larger, the calculation remains precise.
2. Summing the Series Once it can calculate factorials, the script uses them to build a mathematical series. It performs a loop where it adds the fractions of these factorials together (specifically, it sums the reciprocals from 0 up to 11). By adding these progressively smaller pieces together, the script converges on the value of Euler's number.
The Final Result
When you run the script on your platform, it executes these calculations instantly and displays a pop-up alert. This alert shows the resulting constant rounded to eight decimal places. Essentially, the script serves as a precise mathematical engine that "builds" a fundamental mathematical constant from scratch every time it is triggered.
//+------------------------------------------------------------------+
//| Euler.mq4 |
//| Copyright © 2008, Nazariy S. (WWer) |
//| E-mail: nazariy@i.ua, skype: nazariy1309 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Nazariy S. (WWer)"
#property link "E-mail: nazariy@i.ua, Skype: nazariy1309"
double Factorial(int value)
{
if(value<=1) return(1);
double result=1.0;
for(;value!=1;value--)
result*=value;
return(result);
}
double getE()
{
double e=0.0;
for(int n=0; n<12; n++)
e+=1/Factorial(n);
return(e);
}
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
Alert(DoubleToStr(getE(),8));
//----
return(0);
}
//+------------------------------------------------------------------+
Comments