Hi all,
I just write a procedure to decompose an unsigned integer to prime numbers. it will work normally if I define the data type as "int", if I change it to "long", result will be wrong. I don't know why.
BTW, I used Win-TC as my compiler.
Code as below:
#include "stdio.h"
#define True 0xff
#define False 0x00
char DividerIsPrime(unsigned long data);
void CheckIfDataCanBeExtracted(unsigned long data);
main()
{
unsigned long data;
printf("please input data:");
scanf("%d",&data);
printf("\n%d=",data);
CheckIfDataCanBeExtracted(data);
// printf("%d",sizeof(short));
getch();
}
void CheckIfDataCanBeExtracted(unsigned long data)
{
unsigned long divider,temp,data1;
data1=data;
for(divider=2;divider<=data;divider++)
{
temp=data1%divider;
if(temp) {continue; }
if(DividerIsPrime(divider)) {
data1 = data1/divider;
printf("%d",divider);
if(data1==1) break;
else {printf("*"); divider--;}
}
}
return;
}
/* Check if this number is a prime number */
char DividerIsPrime(unsigned long data)
{
unsigned long divider;
char status=True;
for(divider=2;divider<data;divider++)
{
if(data%divider) status=True;
else status=False;
}
return status;
}
Thanks for Paul's help, I know where is wrong. %d should be replaced by %ld.