我试图找到梅森素数

时间:2014-02-19 09:22:05

标签: c++

#include<iostream>
#include <time.h>
#include <sys/timeb.h>

using namespace std;

bool IsPrime( unsigned long long n );
unsigned long long pow2( unsigned n );
unsigned long long Mersenne( unsigned n);
unsigned long long Sqrt ( unsigned long long n );
bool LucasLehmer ( unsigned long long n );
double TIME ( void );

//------------------------------------------------------------
int main()
{
      for ( unsigned long long n = 1; n < 10; n++)
      {
           if ( IsPrime(n))
                cout <<" IsPrime "<< endl;
      }
      return 0;
}
//------------------------------------------------------------

bool IsPrime( unsigned long long n )
{
    if  (n<= 2 ||  n%2==0) 
        return n==2

    for ( unsigned long long  i=3; i<n; i+=2)
    {
         if (n%i==0)
             return false;
         else
             return true;
    }
};
//------------------------------------------------------------

unsigned long long pow2( unsigned n )
{
       unsigned long long ret= 1;
       return ret<< n;
}
//------------------------------------------------------------

unsigned long long Mersenne( unsigned n)
{
      return  n-1;
}
//------------------------------------------------------------

unsigned long long sqrt ( unsigned long long n )
{
       return sqrt(n);
}
//------------------------------------------------------------

bool LucasLehmer ( unsigned long long n )
{
      if (n=2) 
          return true;

      unsigned long long s=4;
      const unsigned long long mp=Mersenne(n);
      for (unsigned long long i=3; i=n; i=++);
}
//------------------------------------------------------------

unsigned long long Mersenne( unsigned n)
{
    return  n-1;
}
//------------------------------------------------------------

unsigned long long sqrt ( unsigned long long n )
{
       return sqrt(n);
}
//------------------------------------------------------------

bool LucasLehmer ( unsigned long long n )
{
       if (n=2) 
           return true;
       unsigned long long s=4;
       const unsigned long long mp=Mersenne(n);
       for (unsigned long long i=3; i=n; i=++);
}
//------------------------------------------------------------

double TIME ( void )
{
      struct timeb t;
      ftime(&t);
      return ( ( (1000.0 * t.time) + t.millitm ) );
}

编译代码时,我不断收到这些错误消息:

error: expected initializer before ‘if’
 if  (n<= 2 ||  n%2==0) return n==2)
error: ‘i’ does not name a type
  for ( unsigned long long  i=3; i<n; i+=2)
                                 ^
error: ‘i’ does not name a type
  for ( unsigned long long  i=3; i<n; i+=2)
                                      ^
error: expected primary-expression before ‘)’ token
   for (unsigned long long i=3; i=n; i=++);
                                     ^

我刚开始上这门课,这是我的第一份作业,我们将非常感谢任何帮助。

*我正在使用GNU GCC编译器

4 个答案:

答案 0 :(得分:2)

你在许多地方写过if(n=2)。您需要if(n==2)

答案 1 :(得分:2)

在isPrime定义之前,您缺少大括号:

bool IsPrime( unsigned long long n )
{
 if  ((n<= 2 ||  n%2==0) return n==2;)
    for ( unsigned long long  i=3; i<n; i+=2)
       {
         if (n%i==0)
           return false;
         else
           return true;
       };
 }

答案 2 :(得分:1)

bool IsPrime( unsigned long long n )
{
    if  (n%2==0) 
        return false;
    for ( unsigned long long  i=3; i<n; i+=2)
       {
         if (n%i==0)
           return false;
       }
    return true;
}

您的IsPrime方法有一些错误。对于n = 1,根据您的代码,它应该是素数。我修改了那部分。

答案 3 :(得分:1)

IsPrime缺少身体周围的牙箍。

isPrime的第一行以)结尾,应为;

您正在使用赋值运算符=进行比较。

这一行是无稽之谈,无法建议更正:

for (unsigned long long i=3; i=n; i=++);

如果你写了

for (unsigned long long i=3; i=n; i++);

它在语法上是正确的,但仍然没有意义 - 它要么永远不做,要么根本不做,取决于n的值。