رقم كارول

رقم كارول
جربه على ممارسة GfG #practiceLinkDiv { العرض: لا شيء! مهم؛ }

رقم كارول هو عدد صحيح من النموذج 4 ن - 2 (ن+1) - 1. الصيغة المكافئة هي (2 ن -1) 2 - 2.
خاصية مثيرة للاهتمام: 
بالنسبة لـ n > 2، التمثيل الثنائي لرقم كارول n هو n-2 واحد متتالي وهو صفر واحد في المنتصف وn + 1 واحد متتالي آخر. مثال n = 4 رقم كارول هو 223 وثنائي 223 هو 11011111 هنا n-2 = 4-2 = 2 متتالية في البداية ثم 0 منفردة في المنتصف ثم n + 1 = 4 + 1 = 5 متتالية بعد ذلك.
بالنظر إلى رقم n، فإن المهمة هي العثور على رقم كارول n. أرقام الترانيم القليلة الأولى هي -1 7 47 223 959... إلخ.

أمثلة :  

Input : n = 2 Output: 7 Input : n = 4 Output: 223 
Recommended 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: O(log n) لوظيفة الأسرى

المساحة المساعدة: يا(1)