This is a discussion on Question about code within the C Programming forums, part of the General Programming Boards category; hello,everyone,i just saw a program in c that computes all carmichael numbers to a MAXNUMB defined constant.The whole program up ...

hello,everyone,i just saw a program in c that computes all carmichael numbers to a MAXNUMB defined constant.The whole program up on computing the carmichael numbers is based on Korselt's criterion(see link above in wiki).Even though i understand the general philosophy of the algorithm i still have some questions about it..I want someone to explain me the red code actually if possible..It's where this temp confuses me a bit..It's maybe obvious or ridiculous to explain but at this time at night i thought i will give it a try and post it here.Thanks in advance!

Code:
```#include <stdio.h>
#include <math.h>
#define MAXNUMB 1000000

main()
{
int N,d,a,b,temp;

for (N=561;N<=MAXNUMB;N+=2) {
a=0;
b=0;
for (d=3;d<=sqrt(N);d+=2)
if  (N%(d*d)==0)
a=1;
if (a==0){
temp=N;
for (d=3;d<=(temp);d+=2) {
if (temp%d==0){
if ((N-1)%(d-1)!=0)
a=1;
else{
temp/=d;
b++;
}
}
}
}
if ((a==0)&&(b>1)){
temp=N;
printf("%d is a Carmichael number: ", N);
for (d=3; d<=(temp);d+=2){
if (temp%d==0){
printf("%d ", d);
temp/=d;
}
}
printf("\n");
}
}
printf("Done!");```

2. The reason they use temp is because they actually want to do calculations using N, but they need to change that value in subsequent iterations of the for(d=3; ...) loops. They don't want to lose the value in N, so they use temp instead, and modify that. You can try reading the code as though temp were replaced by N everywhere.