Exemplo n.º 1
0
Arquivo: 1.2.6.cpp Projeto: kimihe/ACM
int main(int argc, const char * argv[])
{
    int number = 0;
    while (scanf("%d",&number) != EOF)
    {
        if (number == 0)
            exit(0);
        int res = lowestBit(number);
        printf("%d\n", res);
    }
    return 0;
}
/**
 * <p>Factorisation using Pollard's rho method</p>
 * <p>
 *   Advanced algorithms, KTH – Project 1
 *   DD2440 “avag11”
 * </p>
 * <p>
 *   Enter large integer that you want factorised into primes, exit with empty line
 *   Each prime will be printed on a separate line each, directly after your input,
 *   when the factorisation is complete.
 * </p>
 * 
 * @author  Mattias Andrée, [email protected], 900223-3253
 * @author  Joel Mickelin,  [email protected],   880729-0070
 *
 * @param  argc  Number of elements in argv
 * @param  argv  The command line used to start the program: the program followed by options
 */
int main(int argc, String argv[])
{
    unused argc;
    unused argv;


    //Creating seed list, evens are prime, odds are composites
    seeds = malloc((SEED_LIMIT + 1) * sizeof(llong));
    *(seeds + 1) = 10000;
    *(seeds + 2) = 15319;
    *(seeds + 3) = 10001;
    *(seeds + 4) = 15401;
    *(seeds + 5) = 10002;
    *(seeds + 6) = 10007;
    *(seeds + 7) = 10003;
    *(seeds + 8) = 22541;
    *(seeds + 9) = 10004;
    *seeds = 0;


    //Integers
    Bignum q; mpz_init(q);              //Quotient
    Bignum r; mpz_init(r);              //Remainder
    int rootOrder;                      //The order of the integer = max {i : x↑i = integer}
    int fx;                             //The number of factors of small primes
    Bignum integer; mpz_init(integer);  //Integer to factorise

    //Prime factor buffer
    long* bufptr = malloc(sizeof(long));
    Buffer buffer = new_Buffer(1);

    //Constants, it is very important for speed to test this primes
    Bignum _3;  mpz_init_set_ui(_3, 3);
    Bignum _5;  mpz_init_set_ui(_5, 5);
    Bignum _7;  mpz_init_set_ui(_7, 7);

    
    #define appendFactors(X)  appendToBuffer(buffer, (*bufptr)++, X)
    #define  printFactors     printBuffer(buffer, *bufptr)


    while (readBignum(integer))
    {
        *bufptr = 0;
	free(*buffer);
	*buffer = malloc(0);

	//Remove all factors of 2
	if ((fx = lowestBit(integer)) > 0)
	{
	    shiftRight(integer, integer, fx);

	    if (fx & 1)   appendFactors(STR_1(2));
	    if (fx & 2)   appendFactors(STR_2(2));
	    if (fx & 4)   appendFactors(STR_4(2));
	    if (fx & 8)   appendFactors(STR_8(2));
	    if (fx & 16)  appendFactors(STR_16(2));
	    if (fx & 32)  appendFactors(STR_32(2));
	    if (fx & 64)  appendFactors(STR_64(2));
	    //limit, 127 factors of 2, we will not reach that

	    if (equals(integer, 1))
	    {
	        printFactors;
	        printf("\n");
		continue;
	    }
	}

	//Remove all factors of X
        #define factorIt(X)                                          \
	if (fx = contDiv(integer, integer, _##X, null, null, 0))     \
	    {							     \
		if (fx & 1)   appendFactors(STR_1(X));		     \
		if (fx & 2)   appendFactors(STR_2(X));		     \
		if (fx & 4)   appendFactors(STR_4(X));		     \
		if (fx & 8)   appendFactors(STR_8(X));		     \
		if (fx & 16)  appendFactors(STR_16(X));		     \
		if (fx & 32)  appendFactors(STR_32(X));		     \
		if (fx & 64)  appendFactors(STR_64(X));		     \
								     \
		if (equals(integer, 1))				     \
		{						     \
		    printFactors;				     \
		    printf("\n");				     \
		    continue;					     \
		}						     \
	    }

	factorIt(3)
	factorIt(5)
	//factorIt(7)

	if (isPrime(integer))
	{
	    printFactors;
	    printf("%s\n\n", bignumToString(integer));
	} 
	else if (factorisePollardsRho(integer, 0, buffer, bufptr, 1))
	{
	    printFactors;
	    printf("\n");
	}
	else
	    printf(FAIL);
    }

    free(bufptr);
    free(buffer);

    successful;
}