title: How to Calculate 2^x mod n = 1 in less than 1 second
body: <p>I want to write the program that Calculate <code>2^x mod n = 1</code> we have <code>n</code> is an <code>integer</code> but, we should calculate <code>x</code>.I wrote the code but my code work too slow in big n.Can you suggest me a good way that work less than 1 second to solve this problem.<br>
  here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  int main()
  {
      long long int n,cntr=1,cheak;
      cin &gt;&gt; n;
      while (1)
      {
          if (n % 2 == 0)
          {
              break;
          }
          cheak=pow(2, cntr);
          if (cheak % n == 1)
              break;
          cntr++;
      }
      cout &lt;&lt; cntr &lt;&lt; endl;
  } 
  </code></pre>