- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find expected value of given equation for random numbers in Python

Suppose we have a number n. Consider x = rand() mod n, where rand() function generates integers between 0 and 10^100 (both inclusive) uniformly at random. And

$$Y = \sqrt{x+\sqrt{x+\sqrt{x+\sqrt{x+...}}}}$$

We have to find the expected value of Y. The value of n will be in range 1 and 5*10^6.

So, if the input is like n = 5, then the output will be 1.696

To solve this, we will follow these steps −

- err := 2235.023971557617
- max_n := 5 * 10^6
- pref := a list initially contains a single 0
- for i in range 1 to 5 * 10^6, do
- insert (last item of pref + (1 +(4*i + 1)^0.5) * 0.5 at the end of pref

- if n < max_n, then
- return pref[n - 1] / n

- otherwise,
- total :=(4 *(n - 1) + 5)^1.5 / 6 - 5^1.5 / 6 - err
- ans := 0.5 + total /(2 * n)
- return ans

## Example

Let us see the following implementation to get better understanding −

def solve(n): err = 2235.023971557617 max_n = 5 * 10**6 pref = [0] for i in range(1, 5 * 10**6): pref.append(pref[-1] + (1 + (4 * i + 1)**0.5) * 0.5) if n < max_n: return pref[n - 1] / n else: total = (4 * (n - 1) + 5)**1.5 / 6 - 5**1.5 / 6 - err ans = 0.5 + total / (2 * n) return ans n = 5 print(solve(n))

## Input

5

## Output

1.69647248786

- Related Articles
- Program to find out the value of a given equation in Python
- Program to find expected value of maximum occurred frequency values of expression results in Python
- Program to find max value of an equation in Python
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Program to find kpr sum for all queries for a given list of numbers in Python
- Program to find maximum possible value of an expression using given set of numbers in Python
- Program to find expected growth of virus after time t in Python
- Program to find value for which given array expression is maximized in Python
- Python program to generate random numbers within a given range and store in a list?
- Program to find bitwise AND of range of numbers in given range in Python
- Program to find product of few numbers whose sum is given in Python
- Write a program in Python to find the index for NaN value in a given series
- Program to find last digit of the given sequence for given n in Python
- Program to find number of expected moves required to win Lotus and Caterpillar game in Python
- Program to find expected number of shuffle required to sort the elements of an array in Python

Advertisements