# c++ - problem - smallest sequence with given primes leetcode

## 有效地获得给定数字的所有除数 (6)

`for( int i = 1; i * i <= num; i++ )` {/ * upto sqrt是因为当数字被i除以时，也会找到sqrt之后的所有数字。 例如，如果数字除以5时数字是90，那么你也可以看到90/5 = 18，其中18也划分数字，但是当数字是完美平方时，则num / i == i因此只有i是因子* /

``````for (int i = 1; i <= num; ++i){
if (num % i == 0)
cout << i << endl;
}
``````

1. 通过此solution查找给定数字的所有主要因子。
2. 获得这些素因子的所有可能组合。

``````int square_root = (int) sqrt(num) + 1;
for (int i = 1; i < square_root; i++) {
if (num % i == 0&&i*i!=num)
cout << i << num/i << endl;
if (num % i == 0&&i*i==num)
cout << i << '\n';
}
``````

``````enter code here
#include <iostream>
#include <stdio.h>
#include <math.h>

bool loop = true;
int x,y,z=1;
using namespace std;

cout << "\n Inserisci il numero da anaizzare : "; #it insetrs the number
that will be analised
cin >> x;
}
void algoritmo(){
for (int y=1;y<=x;y++){
if (x%y!=0){
y++;
}
else{
cout << " " << y;
}
}
}

int main(){
while (loop == true){
algoritmo(); #this is the void the algorithm
}
return 0;
}
``````

======

``````00
01
10
11
20
21
``````

``````#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

#define pii pair<int, int>

#define MAX 46656
#define LMT 216
#define LEN 4830
#define RNG 100032

unsigned base[MAX / 64], segment[RNG / 64], primes[LEN];

#define sq(x) ((x)*(x))
#define mset(x,v) memset(x,v,sizeof(x))
#define chkC(x,n) (x[n>>6]&(1<<((n>>1)&31)))
#define setC(x,n) (x[n>>6]|=(1<<((n>>1)&31)))

// http://zobayer.blogspot.com/2009/09/segmented-sieve.html
void sieve()
{
unsigned i, j, k;
for (i = 3; i<LMT; i += 2)
if (!chkC(base, i))
for (j = i*i, k = i << 1; j<MAX; j += k)
setC(base, j);
primes[0] = 2;
for (i = 3, j = 1; i<MAX; i += 2)
if (!chkC(base, i))
primes[j++] = i;
}

//http://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/
vector <pii> factors;
void primeFactors(int num)
{
int expo = 0;
for (int i = 0; primes[i] <= sqrt(num); i++)
{
expo = 0;
int prime = primes[i];
while (num % prime == 0){
expo++;
num = num / prime;
}
if (expo>0)
factors.push_back(make_pair(prime, expo));
}

if ( num >= 2)
factors.push_back(make_pair(num, 1));

}

vector <int> divisors;
void setDivisors(int n, int i) {
int j, x, k;
for (j = i; j<factors.size(); j++) {
x = factors[j].first * n;
for (k = 0; k<factors[j].second; k++) {
divisors.push_back(x);
setDivisors(x, j + 1);
x *= factors[j].first;
}
}
}

int main() {

sieve();
int n, x, i;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
primeFactors(x);
setDivisors(1, 0);
divisors.push_back(1);
sort(divisors.begin(), divisors.end());
cout << divisors.size() << "\n";
for (int j = 0; j < divisors.size(); j++) {
cout << divisors[j] << " ";
}
cout << "\n";
divisors.clear();
factors.clear();
}
}
``````

``````factors[0].first=2, factors[0].second=2
factors[1].first=3, factors[1].second=1
``````

``````//Try this,it can find divisors of verrrrrrrrrry big numbers (pretty efficiently :-))
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<conio.h>

using namespace std;

vector<double> D;

void divs(double N);
double mod(double &n1, double &n2);
void push(double N);
void show();

int main()
{
double N;
cout << "\n Enter number: "; cin >> N;

divs(N); // find and push divisors to D

cout << "\n Divisors of "<<N<<": "; show(); // show contents of D (all divisors of N)

_getch(); // used visual studio, if it isn't supported replace it by "getch();"
return(0);
}

void divs(double N)
{
for (double i = 1; i <= sqrt(N); ++i)
{
if (!mod(N, i)) { push(i); if(i*i!=N) push(N / i); }
}
}

double mod(double &n1, double &n2)
{
return(((n1/n2)-floor(n1/n2))*n2);
}

void push(double N)
{
double s = 1, e = D.size(), m = floor((s + e) / 2);
while (s <= e)
{
if (N==D[m-1]) { return; }
else if (N > D[m-1]) { s = m + 1; }
else { e = m - 1; }
m = floor((s + e) / 2);
}
D.insert(D.begin() + m, N);
}

void show()
{
for (double i = 0; i < D.size(); ++i) cout << D[i] << " ";
}
``````