This doesn't just look weird. Ignoring pow()'s type requirements, it's just wrong.
1. = is the assignment operator, not the comparison operator. You can't assign to the return value of a function.
2. The expression (x==1 %y) still doesn't make sense. % has little to do with congruence from mathematics. x%y is merely the remainder of dividing x by y. (x==1%y) is equivalent to (x==(1%y)), which of course is equivalent to (x==1).
The proper way to write this check (again, ignoring pow()'s type requirements) would be (pow(i+1,i-1)%i==1).
Using Fermat's little theorem doesn't give you any advantage over, say, trial division. It's much worse, actually, because you have to test all integers in [2;n], and pow() is much slower than %.
Since you have to generate many consecutive primes, it would probably be best to use a sieve.
1 2 3 4
|
for (int i=0; i<n, i++;)
{
sum=sum+i*small_fermat(i);
}
|
Three things:
1. Check your semicolons in the for header.
2. You're supposed to get the first 2M prime numbers, not the prime numbers below 2M.
3. The way you're getting the sum is a bit unintuitive. Most people will have to look twice before they realize what the line does. This would make sense if some numbers were "more prime" than others, and you want to get like an integer integral; or if the language didn't have decision structure. But neither of those is the case, so just do
1 2
|
if (is_prime(i))
sum+=i;
|