# Carol Number

A Carol number is an integer of the form 4^{n} – 2^{(n+1)} – 1. An equivalent formula is (2^{n}-1)^{2} – 2.

**An Interesting Property : **

For n > 2, the binary representation of the n-th Carol number is n-2 consecutive one’s, a single zero in the middle, and n + 1 more consecutive one’s. Example, n = 4 carol number is 223 and binary of 223 is 11011111, here n-2 = 4-2 = 2 consecutive one’s in starting then single 0 in middle and then n + 1 = 4 + 1 = 5 consecutive one’s after it.

Given a number n, the task is to find the n’th Carol Number. First few carol numbers are 1, 7, 47, 223, 959… etc.

**Examples :**

Input : n = 2 Output: 7 Input : n = 4 Output: 223

## C++

`// C++ program to find n'th Carol number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find n'th carol number ` `int` `carol(` `int` `n) ` `{ ` ` ` `int` `result = ` `pow` `(2, n) - 1; ` ` ` `return` `result * result - 2; ` `} ` ` ` `// Driver program to ru the case ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `cout << carol(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find n'th Carol number ` `def` `carol(n): ` ` ` `# a**b is a ^ b in python ` ` ` `result ` `=` `(` `2` `*` `*` `n) ` `-` `1` ` ` `return` `result ` `*` `result ` `-` `2` ` ` `# driver program to run the case ` `n ` `=` `4` `print` `carol(n) ` |

*chevron_right*

*filter_none*

## Java

`/* Java program to find n'th Carol number */` `class` `GFG { ` ` ` `static` `int` `carol(` `int` `n) ` ` ` `{ ` ` ` `double` `tmp = Math.pow(` `2` `, n) - ` `1` `; ` ` ` `return` `(` `int` `)tmp; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.println(carol(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`/* C# program to find n'th Carol number */` `using` `System; ` ` ` `class` `GFG { ` ` ` `static` `int` `carol(` `int` `n) ` ` ` `{ ` ` ` `int` `result = (` `int` `)Math.Pow(2, n) - 1; ` ` ` `return` `result * result - 2; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` `Console.WriteLine(carol(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// n'th Carol number ` ` ` `// Function to find ` `// n'th carol number ` `function` `carol(` `$n` `) ` `{ ` ` ` `$result` `= pow(2, ` `$n` `) - 1; ` ` ` `return` `$result` `* ` `$result` `- 2; ` `} ` ` ` `// Driver Code ` `$n` `= 4; ` `echo` `carol(` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

223

Reference:

https://en.wikipedia.org/wiki/Carol_number

This article is contributed by **Shashank Mishra ( Gullu )**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Count number of triplets with product equal to given number with duplicates allowed
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Given number of matches played, find number of teams in tournament
- Find the smallest number whose digits multiply to a given number n
- Querying maximum number of divisors that a number in a given range has
- Program to calculate the number of odd days in given number of years
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Build Lowest Number by Removing n digits from a given number
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Count number of ways to divide a number in 4 parts
- Print a number strictly less than a given number such that all its digits are distinct.
- Count number of digits after decimal on dividing a number