mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-05 23:28:34 +08:00
style: format code
This commit is contained in:
@@ -14,14 +14,16 @@ using namespace std;
|
||||
// for multiplication.
|
||||
// This function may value of res_size
|
||||
// and returns the new value of res_size
|
||||
int multiply(int x, int res[], int res_size) {
|
||||
int multiply(int x, int res[], int res_size)
|
||||
{
|
||||
|
||||
// Initialize carry
|
||||
int carry = 0;
|
||||
|
||||
// One by one multiply n with
|
||||
// individual digits of res[]
|
||||
for (int i = 0; i < res_size; i++) {
|
||||
for (int i = 0; i < res_size; i++)
|
||||
{
|
||||
int prod = res[i] * x + carry;
|
||||
|
||||
// Store last digit of
|
||||
@@ -34,7 +36,8 @@ int multiply(int x, int res[], int res_size) {
|
||||
|
||||
// Put carry in res and
|
||||
// increase result size
|
||||
while (carry) {
|
||||
while (carry)
|
||||
{
|
||||
res[res_size] = carry % 10;
|
||||
carry = carry / 10;
|
||||
res_size++;
|
||||
@@ -48,18 +51,19 @@ void power(int x, int n)
|
||||
{
|
||||
|
||||
//printing value "1" for power = 0
|
||||
if(n == 0 ){
|
||||
cout<<"1";
|
||||
if (n == 0)
|
||||
{
|
||||
cout << "1";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int res[MAX];
|
||||
int res_size = 0;
|
||||
int temp = x;
|
||||
|
||||
// Initialize result
|
||||
while (temp != 0) {
|
||||
while (temp != 0)
|
||||
{
|
||||
res[res_size++] = temp % 10;
|
||||
temp = temp / 10;
|
||||
}
|
||||
@@ -75,7 +79,8 @@ void power(int x, int n)
|
||||
}
|
||||
|
||||
// Driver program
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
int exponent, base;
|
||||
printf("Enter base ");
|
||||
scanf("%id \n", &base);
|
||||
|
||||
@@ -6,7 +6,7 @@ using namespace std;
|
||||
// Declaring variables for maintaing prime numbers and to check whether a number is prime or not
|
||||
bool isprime[1000006];
|
||||
vector<int> prime_numbers;
|
||||
vector<pair<int,int> > factors;
|
||||
vector<pair<int, int>> factors;
|
||||
|
||||
// Calculating prime number upto a given range
|
||||
void SieveOfEratosthenes(int N)
|
||||
@@ -14,21 +14,20 @@ void SieveOfEratosthenes(int N)
|
||||
// initializes the array isprime
|
||||
memset(isprime, true, sizeof isprime);
|
||||
|
||||
for(int i=2; i<=N ; i++)
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
if(isprime[i])
|
||||
if (isprime[i])
|
||||
{
|
||||
for(int j=2*i; j<=N; j+=i)
|
||||
isprime[j]=false;
|
||||
for (int j = 2 * i; j <= N; j += i)
|
||||
isprime[j] = false;
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=2; i<=N; i++)
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
if(isprime[i])
|
||||
if (isprime[i])
|
||||
prime_numbers.push_back(i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Prime factorization of a number
|
||||
@@ -37,9 +36,9 @@ void prime_factorization(int num)
|
||||
|
||||
int number = num;
|
||||
|
||||
for(int i=0; prime_numbers[i]<=num; i++)
|
||||
for (int i = 0; prime_numbers[i] <= num; i++)
|
||||
{
|
||||
int count=0;
|
||||
int count = 0;
|
||||
|
||||
// termination condition
|
||||
if (number == 1)
|
||||
@@ -47,17 +46,15 @@ void prime_factorization(int num)
|
||||
break;
|
||||
}
|
||||
|
||||
while(number%prime_numbers[i] == 0)
|
||||
while (number % prime_numbers[i] == 0)
|
||||
{
|
||||
count++;
|
||||
number = number/prime_numbers[i];
|
||||
number = number / prime_numbers[i];
|
||||
}
|
||||
|
||||
if(count)
|
||||
factors.push_back(make_pair(prime_numbers[i],count));
|
||||
if (count)
|
||||
factors.push_back(make_pair(prime_numbers[i], count));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -68,16 +65,16 @@ int main()
|
||||
int num;
|
||||
cout << "\t\tComputes the prime factorization\n\n";
|
||||
cout << "Type in a number: ";
|
||||
cin>>num;
|
||||
cin >> num;
|
||||
|
||||
SieveOfEratosthenes(num);
|
||||
|
||||
prime_factorization(num);
|
||||
|
||||
// Prime factors with their powers in the given number in new line
|
||||
for(auto it: factors)
|
||||
for (auto it : factors)
|
||||
{
|
||||
cout<<it.first<<" "<<it.second<<endl;
|
||||
cout << it.first << " " << it.second << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -17,12 +17,16 @@ int isprime[MAX];
|
||||
* This is the function that finds the primes and eliminates
|
||||
* the multiples.
|
||||
*/
|
||||
void sieve(int N) {
|
||||
void sieve(int N)
|
||||
{
|
||||
isprime[0] = 0;
|
||||
isprime[1] = 0;
|
||||
for (int i = 2; i <= N; i++) {
|
||||
if (isprime[i]) {
|
||||
for (int j = i * 2; j <= N; j += i) {
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
if (isprime[i])
|
||||
{
|
||||
for (int j = i * 2; j <= N; j += i)
|
||||
{
|
||||
isprime[j] = 0;
|
||||
}
|
||||
}
|
||||
@@ -32,9 +36,12 @@ void sieve(int N) {
|
||||
/*
|
||||
* This function prints out the primes to STDOUT
|
||||
*/
|
||||
void print(int N) {
|
||||
for (int i = 1; i <= N; i++) {
|
||||
if (isprime[i] == 1) {
|
||||
void print(int N)
|
||||
{
|
||||
for (int i = 1; i <= N; i++)
|
||||
{
|
||||
if (isprime[i] == 1)
|
||||
{
|
||||
cout << i << ' ';
|
||||
}
|
||||
}
|
||||
@@ -45,13 +52,16 @@ void print(int N) {
|
||||
* NOTE: This function is important for the
|
||||
* initialization of the array.
|
||||
*/
|
||||
void init() {
|
||||
for (int i = 1; i < MAX; i++) {
|
||||
void init()
|
||||
{
|
||||
for (int i = 1; i < MAX; i++)
|
||||
{
|
||||
isprime[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
int N = 100;
|
||||
init();
|
||||
sieve(N);
|
||||
|
||||
Reference in New Issue
Block a user