Thread: Question about code

  1. #1
    Registered User Aeria Gloris's Avatar
    Join Date
    Nov 2009
    Posts
    4

    Question about code

    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. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about red-black tree and little code fragment.
    By mindvera in forum C++ Programming
    Replies: 0
    Last Post: 07-05-2010, 11:42 AM
  2. Simple question, trying to understand this code...
    By matthayzon89 in forum C Programming
    Replies: 11
    Last Post: 03-23-2010, 01:04 PM
  3. Question related to returning to earlier parts of the code.
    By Cgrasshopper in forum C Programming
    Replies: 2
    Last Post: 03-13-2010, 11:00 PM
  4. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  5. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM