#practiceLinkDiv { 표시: 없음 !중요; }Carol 번호는 4 형식의 정수입니다.N- 2(n+1)- 1. 동등한 공식은 (2N-1)2- 2.
흥미로운 자산:
n > 2인 경우 n번째 캐롤 번호의 이진 표현은 n-2 연속 1이고 중간에 단일 0이 있고 n + 1 연속 연속 1입니다. 예 n = 4 캐롤 번호는 223이고 223의 이진수는 11011111입니다. 여기서 n-2 = 4-2 = 시작에 2개의 연속된 것, 중간에 단일 0, 그 다음 n + 1 = 4 + 1 = 5개의 연속된 것입니다.
숫자 n이 주어지면 작업은 n번째 Carol Number를 찾는 것입니다. 처음 몇 개의 캐롤 번호는 -1 7 47 223 959... 등입니다.
예:
Input : n = 2 Output: 7 Input : n = 4 Output: 223Recommended Practice 캐롤 넘버 시도해 보세요! C++
// C++ program to find n'th Carol number #include 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; }
Python3 # 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))
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)); } }
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.
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 ?> JavaScript <script> /* Javascript program to find n'th Carol number */ function carol(n) { let result = Math.pow(2 n) - 1; return result * result - 2; } let n = 4; document.write(carol(n)); </script>
출력 :
223
시간 복잡도 y: pow 함수의 경우 O(log n)
보조 공간: 오(1)