// primeclone.c - Clone of my TI-83+ program #include unsigned long isprime ( unsigned long x ); void factor( unsigned long thenumber ); int main( int argc, char *argv[] ) { if (argc < 2) { puts("Where's my number?"); return 1; } if (argc > 2) { puts("It's a factoring program; where are you getting these extra options?"); return 1; } unsigned long meow; meow = atoi(argv[1]); factor( meow ); return 0; } unsigned long isprime ( unsigned long x ) { unsigned long i = 3, s = sqrt(x); if (x < 2) return 0; if (x == 2) return 1; if (x % 2 == 0) return 0; for ( ; i <= s ; i += 2 ) if (x % i == 0) return 0; return 1; } void factor( unsigned long thenumber ) { unsigned long prime = 2, exp = 0; printf ("%u: ", thenumber); while( (!isprime(thenumber)) && (thenumber != 1) ) { while( (!(thenumber % prime)) && (thenumber != 1) ) { thenumber /= prime; exp++; } if (exp) printf("%u^%u ", prime, exp); exp = 0; while( !isprime(++prime) ); } if (thenumber != 1) printf("%u^1", thenumber); return; }