void factorize(long n) {

    srand(time(NULL));
    long randomed = rand() % n;
    //printf("%d\n", randomed);

//	printf("%d --- %d\n", n, randomed);
    if (NWD(n, randomed) > 1) {
        printf("%d --- %d\n", n, NWD(n, randomed));
    } else {
        long r = solveDL(randomed, n);
        if (r == -1) {
            factorize(n);
        } else if (r % 2 == 0) {
            if (NWD(n, potega(randomed, r / 2) + 1) > 1) {
                printf("%d --- %d\n", n, (NWD(n, potega(randomed, r / 2) + 1)));
                cout << n << "---" << NWD(n, potega(randomed, r / 2) + 1) << endl;
            } else if (NWD(n, potega(randomed, r / 2) - 1) > 1) {
                printf("%d --- %d\n", n,(NWD(n, potega(randomed, r / 2) - 1)));
            } else {
                factorize(n);
            }
        } else {
            factorize(n);
        }
    }

}
示例#2
0
int main(int argc, const char * argv[]){
	clock_t begin = clock();

    if(argc == 1){
		// standard mode
		mpz_t numbers[NUMBERS];
		list * calculatedFactors[NUMBERS];
		int i;
		mpz_t y;
		mpz_init_set_ui(y, 1);

		for (i = 0; i < NUMBERS; i++){
			mpz_init(numbers[i]);
			mpz_inp_str(numbers[i], stdin, 10);
		}

		for (i = 0; i < NUMBERS; i++) {
			reset_timer();
			list* factors = createList();
			factorize(factors, numbers[i],1, y);
			
			calculatedFactors[i] = factors;
		}

		for (i = 0; i < NUMBERS; i++){
			printFactors(calculatedFactors[i]);
		}

		clock_t end = clock();
		double tdiff = (((double) end) - ((double)begin)) / CLOCKS_PER_SEC;
		printf(", %f\n", tdiff);
		
		return 0;
		
	} else if(strcmp(argv[1], "interactive") == 0){
		// interactive mode
		fprintf(stderr, "Interactive mode!\n");
		
		mpz_t number;
		mpz_init(number);
		list * factors = NULL;
		mpz_t y;
		mpz_init_set_ui(y, 1);

		while (1) {
			mpz_inp_str(number, stdin, 10);
			reset_timer();
			factors = createList();
			factorize(factors, number, 1, y);

			TRACE("PRINTING FACTORS:\n");
			printFactors(factors);
		}

		return 0;
	}
    
    return 1;
}
void factorize(LL n, vector<LL> &divisor) {
	if (n == 1) return;
	if (prime_test(n)) divisor.push_back(n);
	else {
		LL d = n;
		while (d >= n) d = pollard_rho(n, rand() % (n - 1) + 1);
		factorize(n / d, divisor);
		factorize(d, divisor);
	}
}
示例#4
0
/*
 *大整数质因数分解
 *ret存储n的所有质因子,c是任意一个数字
 */
void factorize(ll n, int c, vector<ll> &ret) {
    if (n == 1) return ;
    if (Miller_Rabin(n)) {
        ret.push_back(n);
        return ;
    }
    ll p = n;
    while (p >= n) p = Pollard_rho(p, c--);
    factorize(p, c, ret);
    factorize(n/p, c, ret);
}
int SNE09() {
    introSNE09();
	factorize(12);
    factorize(91);
    factorize(143);
    factorize(1737);
    factorize(1859);
    
    system("PAUSE");
    return 0;
};
static void buildbox(int nnode,ivec nbox,matrix box)
{
  ivec *BB,bxyz;
  int  i,j,m,n,n3,ny,*fx,*fy,nbb;
  
  n3 = ipow(nnode,3)*6;
  snew(BB,n3);
  nbb=0;
  snew(fx,nnode+1);
  snew(fy,nnode+1);
  factorize(nnode,fx);
  for(i=0; (i<=nnode); i++) {
    for(m=1; (m<=fx[i]); m++) {
      bxyz[XX] = ipow(i,m);
      ny = nnode/bxyz[XX];
      factorize(ny,fy);
      for(j=0; (j<=ny); j++) {
	for(n=1; (n<=fy[j]); n++) {
	  bxyz[YY] = ipow(j,n);
	  bxyz[ZZ] = ny/bxyz[YY];
	  if (bxyz[ZZ] > 0) {
	    nbb = add_bb(BB,nbb,bxyz);
	  }
	}
      }
    }
  }
  /* Sort boxes and remove doubles */
  qsort(BB,nbb,sizeof(BB[0]),iv_comp);
  j = 0;
  for(i=1; (i<nbb); i++) {
    if ((BB[i][XX] != BB[j][XX]) || 
	(BB[i][YY] != BB[j][YY]) || 
	(BB[i][ZZ] != BB[j][ZZ])) {
      j++;
      copy_ivec(BB[i],BB[j]);
    }
  }
  nbb = ++j;
  /* Sort boxes according to weight */
  copy_mat(box,BOX);
  qsort(BB,nbb,sizeof(BB[0]),w_comp);
  for(i=0; (i<nbb); i++) {
    fprintf(stderr,"nbox = %2d %2d %2d [ prod %3d ] area = %12.5f (nm^2)\n",
	    BB[i][XX],BB[i][YY],BB[i][ZZ],
	    BB[i][XX]*BB[i][YY]*BB[i][ZZ],
	    box_weight(BB[i],box));
  }
  copy_ivec(BB[0],nbox);
  sfree(BB);
  sfree(fy);
  sfree(fx);
}
示例#7
0
文件: factor.c 项目: alexlibi/save
// Liefert die Primfaktorzerlegung einer Zahl als String.
char *factorize(mpz_t number)
{
	// Primtest (Miller-Rabin).
	if (mpz_probab_prime_p(number, 10) > 0)
		return mpz_get_str(NULL, 10, number);

	mpz_t factor, cofactor;
	mpz_init(factor);
	mpz_init(cofactor);
	char *str1, *str2, *result;
	int B1 = INITB1, B2 = INITB2, curves = INITCURVES;

	// Zunaechst eine einfache Probedivision.
	trial(number, factor, 3e3);
	if (mpz_cmp_si(factor, 1) == 0)
	{
		// Zweite Strategie: Pollard-Rho.
		do
		{
			rho(number, factor, 4e4);
		} while (mpz_cmp(factor, number) == 0);
		// Falls immer noch kein Faktor gefunden wurde, mit ECM fortfahren.
		while (mpz_cmp_si(factor, 1) == 0)
		{
			ecm(number, factor, B1, B2, curves);
			if (mpz_cmp(factor, number) == 0)
			{
				mpz_set_si(factor, 1);
				B1 = INITB1;
				B2 = INITB2;
				curves = INITCURVES;
				continue;
			}
			// Anpassung der Parameter.
			B1 *= 4;
			B2 *= 5;
			curves = (curves * 5) / 2;
		}
	}

	mpz_divexact(cofactor, number, factor);
	str1 = factorize(factor);
	str2 = factorize(cofactor);
	result = (char *) malloc(strlen(str1) + strlen(str2) + 4);
	strcpy(result, str1);
	strcat(result, " * ");
	strcat(result, str2);

	mpz_clear(factor);
	mpz_clear(cofactor);
	return result;
}
示例#8
0
void test(){

    std::cout << sum_of_2_numbers(3,5,999) << std::endl;
    std::cout << sum_of_fib(4000000) << std::endl;
    factorize(13195);
    factorize(600851475143);
    std::cout << findPalindrom() << std::endl;
    std::cout << dif(10) << std::endl;
    std::cout.precision(20);
    std::cout << dif(100) << std::endl;
    //std::cout << prime10001() << std::endl;
    std::cout << biggestProduct("/home/paulina/ClionProjects/EulerProb1/Problem8") << std::endl;
}
int main(){
	FILE *output = fopen("output_problem12.txt", "w"); // Writing to output just to be safe
  int *nums = (int *) calloc(sizeof(int)*8000,sizeof(int));
	int *primes, i, input, j;
	int total_divisors = 1;
	j = 0;

	factor *factors = (factor *) NULL;

	primes = (int *) era_sieve(nums, 8000);

	for(i = 7; i<MAX_TESTED; i++){
		input = tri_number(i);
		factors = (factor *) factorize(input, primes);
		while((factors+j)->exponent != -1){
			total_divisors *= ( ((factors+j)->exponent)+1 );
			j++;
		}
		fprintf(output, "%2d has %4d total divisors.\n", input, total_divisors);
		if(total_divisors > 500){
			printf("FOUND IT! %d has %d divisors.\n", input, total_divisors);
			// Will get my attention :)
		}
    free(factors);
		total_divisors = 1;
		j = 0;
	}

  free(primes);
  free(nums);
	fclose(output);

	return 0;
}
示例#10
0
文件: P10110.cpp 项目: LasseD/uva
int main() {  
  long n;
  while(std::cin >> n) {
    if(n == 0)
      return 0;
    std::vector<unsigned int> factors;
    factorize(n, factors);

    // Count divisors using http://mathschallenge.net/library/number/number_of_divisors
    long divisors = 1;
    int count = 1;
    for(unsigned int i = 1; i < factors.size(); ++i) {
      if(factors[i] == factors[i-1])
	++count;
      else {
	divisors *= count+1;
	count = 1;
      }
    }
    if(n > 1)
      divisors *= count+1;
    //std::cerr << "Divisor count: " << divisors << std::endl;
    
    if(divisors % 2 == 0) {
      std::cout << "no" << std::endl;
    }
    else {
      std::cout << "yes" << std::endl;
    }
  }
}
示例#11
0
int main(){
    int i, numUnique = 0, b, a;
    short uniqueNum[10000][30] = {{0}};
    short basePrimeFactor[30] = {0}, curr[30] = {0};

    genPrimes();

    for (a = 2; a <= 100; a++){
        memset(basePrimeFactor, 0, sizeof(short)*30); 
        factorize(a, basePrimeFactor);

        for (b = 2; b <= 100; b++){
            for (i = 0; i < 30; i++){
                curr[i] = basePrimeFactor[i]*b;
            }
            if (!isDuplicate(curr, uniqueNum, numUnique)){
                memcpy(uniqueNum[numUnique], curr, sizeof(short)*30);
                numUnique++;
            }
        }
    }

    printf("%d\n", numUnique);

    return 0;
}
示例#12
0
文件: factor.c 项目: alexlibi/save
// Das Hauptprogramm.
main(int argc, char *argv[])
{
	if (argc == 1)
	{
		printf("Wo ist die Eingabezahl?\n");
		return 1;
	}

	mpz_t number;
	mpz_init(number);

	if (mpz_set_str(number, argv[1], 10) == -1)
	{
		printf("Ungueltige Eingabe.\n");
		mpz_clear(number);
		return 1;
	}
	if (mpz_cmp_si(number, 2) < 0)
	{
		printf("Natuerliche Zahl > 1 erforderlich.\n");
		mpz_clear(number);
		return 1;
	}

	srand(time(0));
	printf("%s\n", factorize(number));
	mpz_clear(number);
	return 0;
}
示例#13
0
文件: main.cpp 项目: cout/clemson
int main(int argc, char *argv[]) {
	if(argc < 2) {
		cout << "Usage: " << argv[0] << "<number>" << endl;
		exit(1);
	}

	int n = atoi(argv[1]);
	factors f = factorize(n);

	if(f.getsign() == 0) cout << "Unable to factor";
	else if(n == 1) cout << "1";
	else {
		int flag = 0;
		for(int j = 0; j < 999; j++) {
			if(f.getdegree(j) != 0) {
				if(flag == 1) {
					cout << '*';
				} else {
					flag = 1;
				}
				cout << primes[j] << '^' << f.getdegree(j);
			}
		}
	}
	cout << endl;

	return 0;
}
示例#14
0
文件: 01_factor.c 项目: wilx/FIT-PA1
/* Faktorizace cisla na prvocinitele. Pokud se podari nalezt delitele zadaneho cisla,
 * jsou tito delitele dale rekurzivne faktorizovani. Naopak, pokud funkce zjisti, ze
 * delitel neexistuje, vypise faktorizovane cislo (je to prvocielny faktor).
 *
 * Promenna first slozi k potlaceni hvezdicky pred prvnim faktorem
 */
void factorize ( int x, int first )
 {
   int i, max = (int) sqrt ( x );
   
   for ( i = max; i >= 2; i -- )
    if ( x % i == 0 )
     {
       factorize ( i, first );
       factorize ( x / i, 0 );
       return;
     }

   if ( ! first ) 
    printf ( " * " );
   printf ( "%d", x );  
 }
示例#15
0
   int ARLambda::lambda( const Vector<double>& a, 
                         const Matrix<double>& Q, 
                         Matrix<double>& F,
                         Vector<double>& s,
                         const int& m )
   {
      if( (a.size()!=Q.rows()) || (Q.rows()!=Q.cols()) ) return -1;
      if( m < 1) return -1;

      const int n = static_cast<int>(a.size());

      Matrix<double> L(n,n,0.0),E(n,m,0.0);
      Vector<double> D(n,0.0),z(n,0.0);
      Matrix<double> Z = ident<double>(n);

      if (factorize(Q,L,D)==0) 
      {      
         reduction(L,D,Z);
         z = transpose(Z)*a;

         if (search(L,D,z,E,s,m)==0) 
         {
            try
            {  // F=Z'\E - Z nxn  E nxm F nxm
               F = transpose( inverseLUD(Z) ) * E;
            }
            catch(...) 
            { return -1; }
         }
      }

      return 0;

   }  // End of method 'ARLambda::lambda()'
示例#16
0
文件: math.cpp 项目: evpo/EncryptPad
      std::vector<Botan::BigInt> factorize(const Botan::BigInt& n_in,
                                           Botan::RandomNumberGenerator& rng)
         {
         Botan::BigInt n = n_in;
         std::vector<Botan::BigInt> factors = remove_small_factors(n);

         while(n != 1)
            {
            if(Botan::is_prime(n, rng))
               {
               factors.push_back(n);
               break;
               }

            Botan::BigInt a_factor = 0;
            while(a_factor == 0)
               {
               a_factor = rho(n, rng);
               }

            std::vector<Botan::BigInt> rho_factored = factorize(a_factor, rng);
            for(size_t j = 0; j != rho_factored.size(); j++)
               {
               factors.push_back(rho_factored[j]);
               }

            n /= a_factor;
            }

         return factors;
         }
示例#17
0
	Point SpaceParam::get(size_t index)
	{
		if (div_points.empty())
		{
			div_points = factorize(n);

			while (space.size() > div_points.size())
			{
				div_points.push_back(1);
			}
		}

		Point point;

		for (size_t i = 0; i < space.size(); i++)
		{
			size_t j = index % div_points[i];
			index /= div_points[i];

			Number num;

			if (div_points[i] == 1)
			{
				num = space[i].min();
			}
			else
			{
				num = space[i].min() + j * (space[i].max() - space[i].min()) / (div_points[i] - 1);
			}

			point.add(num);
		}
		return point;
	}
示例#18
0
int rate2prob (double ** rate_sym, double * freq, double time_step, int no_timesteps, double *** prob_matrix) {

    int i, j, k, clock;
    double sum = 0;
    double VL[N][N], VR[N][N], egvl[N];
    int factorize (double ** rate_sym, double * freq, double VL[N][N],double VR[N][N],double egvl[N]);

    /* factorization */
    factorize (rate_sym, freq, VL, VR, egvl);
    
   
   
    /* the exponent: */
    for (clock=0; clock < no_timesteps; clock++ ) {
	 for (i=0; i<N; i++) {
	     for (j=0; j<N; j++) {
		 prob_matrix[clock][i][j] = 0;
		 for (k=0; k<N; k++) {
		     prob_matrix[clock][i][j] += VL[i][k]*exp(egvl[k]*clock*time_step)*VR[k][j];
		 }
	     }
	 }
	 /* sanity check: is this a prob matrix? */
	 for (j=0; j<N; j++) {
	     sum = 0.0;
	     for (i=0; i<N; i++) {
		 sum +=   prob_matrix[clock][i][j];
	     }
	     if ( fabs (1.0-sum) > 1.e-10) {
		 fprintf (stderr, "Numerical in rate2prob()? \n");
		 fprintf (stderr, "clock %3d   column %2d:  fabs(1.0-column_sum)=%8.1le\n",
			  clock, j, fabs (1.0-sum));
		 exit (1);
	     }
	 }
    }

    

# if 0
    /* at very long times,  each column should be equal to stationary freq */
     clock = no_timesteps-1;
     //clock = (int)(clock*0.75);
     printf ("\n");
 
     for (i=0; i<N; i++) {
	 for (j=0; j<N; j++) {
	     printf ("%6.3lf ", prob_matrix[clock][i][j] );
	 }
	 printf (" **%6.3lf \n", freq[i]);
     }
     printf ("\n");

     exit (1);
# endif
    
     return 0;

}
示例#19
0
// Prime-power factorization
void pp_factorize(vector<long>& factors, long N)
{
  Vec< Pair<long, long> > pf;
  factorize(pf,N); // prime factors, N = \prod_i pf[i].first^{pf[i].second}
  factors.resize(pf.length());
  for (long i=0; i<pf.length(); i++)
    factors[i] = power_long(pf[i].a, pf[i].b); // p_i^e_i
}
示例#20
0
	T radical(const S n) const{
		std::vector<std::pair<int,int> > fct = factorize(n);
		T total=1;
		for (size_t i=0 ; i<fct.size() ; ++i){
			total *= fct[i].first;
		}
		return total;
	}
示例#21
0
bool PARDISOSolver< valueType, zeroBased >::factorize( CompressedSparseMatrix< valueType >& A )
{
    assert( A.matrixType() == SYMMETRIC );
    assert( A.numRows() == m_nRowsA );
    assert( A.numCols() == m_nColsA );

    return factorize( A.values().data() );
}
bool isAbundant(long n)
{
	factorization *f = factorize(n);
	divisors *divs = createDivisors(f);
	long sum = sumDivisors(divs);

	return sum > n;	
}
示例#23
0
template<class zp,class zz> void FindPrimRootT(zp &root, unsigned long e)
{
  zz qm1 = zp::modulus()-1;

  assert(qm1 % e == 0);
  
  vector<long> facts;
  factorize(facts,e); // factorization of e

  root = 1;

  for (unsigned long i = 0; i < facts.size(); i++) {
    long p = facts[i];
    long pp = p;
    long ee = e/p;
    while (ee % p == 0) {
      ee = ee/p;
      pp = pp*p;
    }
    // so now we have e = pp * ee, where pp is 
    // the power of p that divides e.
    // Our goal is to find an element of order pp

    PrimeSeq s;
    long q;
    zp qq, qq1;
    long iter = 0;
    do {
      iter++;
      if (iter > 1000000) 
        Error("FindPrimitiveRoot: possible infinite loop?");
      q = s.next();
      conv(qq, q);
      power(qq1, qq, qm1/p);
    } while (qq1 == 1);
    power(qq1, qq, qm1/pp); // qq1 has order pp

    mul(root, root, qq1);
  }

  // independent check that we have an e-th root of unity 
  {
    zp s;

    power(s, root, e);
    if (s != 1) Error("FindPrimitiveRoot: internal error (1)");

    // check that s^{e/p} != 1 for any prime divisor p of e
    for (unsigned long i=0; i<facts.size(); i++) {
      long e2 = e/facts[i];
      power(s, root, e2);   // s = root^{e/p}
      if (s == 1) 
        Error("FindPrimitiveRoot: internal error (2)");
    }
  }
}
示例#24
0
int factorize(int num0, int N, int *fund, int *pow, int must )
{
  int i;
  int a,b;
  int num;
  int ret;

  /* must exclude  division 0 */
  if (must==0)  return 0;

  if (must==1) {

    for (i=0;i<N;i++) {
      pow[i] = 0;
    }

  }
  else {
    num=num0%must;
    if ( num==0 ) {
      ret=factorize(  must, N, fund, pow, 1);
      if (ret==0) {
	return ret;
      }
    }
    else {
      return 0;
    }
  }

  num=num0/must ;
  for (i=0; i<N; i++) {
    while (1) {
      b = num%fund[i];

      if (b==0) {
	num /= fund[i];
	pow[i]++;
      }
      else {
	break;
      }
    }

  }

  if (num==1) {
    return num0;
  }
  else {
    return 0;
  }

}
示例#25
0
文件: math.cpp 项目: evpo/EncryptPad
      void go() override
         {
         Botan::BigInt n(get_arg("n"));

         std::vector<Botan::BigInt> factors = factorize(n, rng());
         std::sort(factors.begin(), factors.end());

         output() << n << ": ";
         std::copy(factors.begin(), factors.end(), std::ostream_iterator<Botan::BigInt>(output(), " "));
         output() << std::endl;
         }
示例#26
0
/*! \brief Find largest divisor of \p n smaller than \p n*/
static gmx_bool largest_divisor(int n)
{
    int ndiv, *div, *mdiv, ldiv;

    ndiv = factorize(n, &div, &mdiv);
    ldiv = div[ndiv-1];
    sfree(div);
    sfree(mdiv);

    return ldiv;
}
示例#27
0
void PX(get_procmesh_dims_2d)(
    const INT *n, MPI_Comm comm_cart_3d,
    int *p0, int *p1, int *q0, int *q1
    )
{
  int ndims=3, dims[3];

  PX(get_mpi_cart_dims)(comm_cart_3d, ndims, dims);
  *p0 = dims[0]; *p1 = dims[1];
  factorize(n, dims[0], dims[1], dims[2], q0, q1);
}
示例#28
0
CLR_Fact::CLR_Fact(double ** matrix, int dimension)
{
    m_n = dimension;
    m_matrix = matrix;

    m_indizes = new int[m_n];

    for (int i = 0; i < m_n; i++)
        m_indizes[i] = i;

    m_inv = factorize();
}
示例#29
0
	std::size_t div_num_of(T n) {
		if (n == 0 || n == 1)
			return n;

		std::map<T, size_t> v;
		std::size_t div_num = 1;

		factorize(n, v);
		for (auto e: v) { div_num *= (e.second+1); }

		return div_num;
	}
示例#30
0
template<class zz> static void phiNT(zz &phin, vector<zz> &facts, const zz &N)
{
  if (facts.size()==0) factorize(facts,N);

  zz n = N;
  conv(phin,1); // initialize phiN=1
  for (unsigned long i=0; i<facts.size(); i++) {
    zz p = facts[i];
    phin *= (p-1); // first factor of p
    for (n /= p; (n%p)==0; n /= p) phin *= p; // multiple factors of p
  } 
}