We have a stick of length L. The stick got broken at (n-1) randomly chosen points (lengths of parts can be non-integer or floating point numbers also) so we get n parts. We need to find the probability that these n pieces can form a n sided polygon.

**Examples:**

Input : L = 5 n = 3 Output : 0.25 We need to cut rope of length 5 into three parts.

First we need to find the condition when n lengths can form a n sided polygon. Let us consider a triangle, we know for a triangle the length of the largest side must be smaller than the sum of the lengths of other sides. Similarly for a n sided polygon the length of the largest side must be less than the sum of the other (n-1) sides. Why? Suppose we break the stick into two equal halves. We further break one of the halves into (n-1) parts. We can never place them such that they make a closed polygon. (Actually the best we can do is to make 2 parallel lines). So we just need to find the probability that no part has the length greater than or equal to L/2.

Now we need to work on the probability. There are many ways to calculate the required probability we will use a geometric approach. Consider a circle of perimeter L. We place n points on the perimeter. The probability that they lie on the same semicircle is . Please refer this link for more information, let us denote it by P(E).

This probability is actually same as breaking the stick such that at least one part is of length L/2. But we want just the complement of this event hence our answer is

## C++

`// CPP program to find probability that ` `// a rope of length L when broken into ` `// n parts form a polygon. ` `#include<iostream> ` ` ` `using` `namespace` `std; ` ` ` `double` `printProbability(unsigned L, unsigned n) ` `{ ` ` ` `unsigned p = (1 << (n-1)); ` ` ` `return` `1.0 - ((` `double` `)n) / ((` `double` `)p); ` `} ` ` ` `int` `main() ` `{ ` ` ` `unsigned n = 3, L = 5; ` ` ` `cout << printProbability(L, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find probability that ` `// a rope of length L when broken into ` `// n parts form a polygon. ` ` ` `public` `class` `GFG { ` ` ` ` ` `static` `double` `printProbability(` `int` `L, ` `int` `n) ` ` ` `{ ` ` ` `int` `p = (` `1` `<< (n-` `1` `)); ` ` ` `return` `1.0` `- ((` `double` `)n) / ((` `double` `)p); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `3` `, L = ` `5` `; ` ` ` `System.out.println(printProbability(L, n)); ` ` ` ` ` `} ` ` ` `// This Code is contributed by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find probability that ` `# a rope of length L when broken into ` `# n parts form a polygon. ` ` ` ` ` ` ` `def` `printProbability(L, n): ` ` ` ` ` `p ` `=` `(` `1` `<< (n` `-` `1` `)) ` ` ` `return` `1.0` `-` `(` `float` `(n) ` `/` `float` `(p) ) ` ` ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `3` ` ` `L ` `=` `5` ` ` `print` `(printProbability(L, n)) ` ` ` `# this code is contributed by ash264 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find probability ` `// that a rope of length L when ` `// broken into n parts form a polygon. ` `using System; ` ` ` `class` `GFG ` `{ ` `static` `double` `printProbability(` `int` `L, ` `int` `n) ` `{ ` ` ` `int` `p = (` `1` `<< (n - ` `1` `)); ` ` ` `return` `1.0` `- ((` `double` `)n) / ` ` ` `((` `double` `)p); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = ` `3` `, L = ` `5` `; ` ` ` `Console.WriteLine(printProbability(L, n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find probability that ` `// a rope of length L when broken into ` `// n parts form a polygon. ` ` ` `function` `printProbability(` `$L` `, ` `$n` `) ` `{ ` ` ` `$p` `= (1 << (` `$n` `- 1)); ` ` ` `return` `1.0 - (` `$n` `) / (` `$p` `); ` `} ` ` ` `// Driver Code ` `$n` `= 3; ` `$L` `= 5; ` `echo` `printProbability(` `$L` `, ` `$n` `); ` ` ` `// This code is contributed ` `// by Abby_akku ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0.25

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Probability of cutting a rope into three pieces such that the sides form a triangle
- Number of ways to cut a stick of length N into K pieces
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Apothem of a n-sided regular polygon
- Check if an N-sided Polygon is possible from N given angles
- Angle between 3 given vertices in a n-sided regular polygon
- Central angle of a N sided Regular Polygon
- Length of Diagonal of a n-sided regular polygon
- Area of a n-sided regular polygon with given Radius
- Side of a regular n-sided polygon circumscribed in a circle
- Find number of diagonals in n sided convex polygon
- Determine the position of the third person on regular N sided polygon
- Area of a n-sided regular polygon with given side length
- Area of largest Circle inscribe in N-sided Regular polygon
- Find the angle of Rotational Symmetry of an N-sided regular polygon
- Program to find Area of Triangle inscribed in N-sided Regular Polygon
- Number of occurrences of a given angle formed using 3 vertices of a n-sided regular polygon
- Number of cycles formed by joining vertices of n sided polygon at the center
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.