Fastest way to find dividers of a number in C++ - c++

I am trying to do an exercise in which you should find number of dividers of factorial of nth number:
https://www.e-olymp.com/en/problems/124
So here is my code:
int fact(int n){
return (n==1 || n==0) ? 1 : n*fact(n-1);
}
long long int a,b=1,c=0;
cin>>a;
long long int y=fact(a);
while(b!=y){
if(y%b==0){
c++;
}
b++;
}
cout<<c+1<<endl;
But this code takes too much time and I need something quicker. Don't give code, algorithm will be enough.

In this task 1 <= N <= 45. Obviously, N is too large to calculate it directly.
You should implement another approach. Just iterate from the 1 to N and do the prime factorization for every i, 1 < i <= N. Then you can easily find the prime factorization of N!, just merge factorization of every i, 1 < i <= N. After that calculate the total number of divisors using previously calculated factorization.
Example for 6!:
2 = 2
3 = 3
4 = 2 ^ 2
5 = 5
6 = 3 * 2
So:
6! = 2 ^ 4 * 3 ^ 2 * 5
And the number of divisors:
(4 + 1) * (2 + 1) * (1 + 1) = 30

Use a bit of mathematics.
If you know the prime factors of a number, it is trivial to calculate the number of divisors.
And it's easy to find the prime factors of a factorial. Without calculating the factorial itself.

Related

Find minimum prime numbers which sum to a given value

I want to find the minimum set of prime numbers which would sum to a given value e.g. 9 = 7 + 2 (not 3+3+3).
I have already generated a array of prime numbers using sieve of eratosthens
I am traversing the array in descending order to get the array largest prime number smaller than or equal to given number. This works great if the number is odd.
But fails for even numbers e.g 122 = 113 + 7 + 2 but 122 = 109 +13.
From Golbach's Conjecture we know that any even number can be represented as two sum of two prime numbers. So if a number is even we can directly return 2 as output.
But I am trying to figure out a way other than brute force to find minimum prime numbers.
Although your question didn't say so, I assume you are looking for the set of primes that has the smallest cardinality.
If n is even, then consider the primes p in order, 2, 3, 5, …; eventually n - p will be prime, so n is the sum of two primes. This process typically converges very quickly, with the smaller of the two primes seldom larger than 1000 (and usually much smaller than that).
If n is odd, and n - 2 is prime, then n is the sum of the primes 2 and n - 2.
If n is odd, and n - 2 is not prime, then n - 3 is even and can be written as the sum of two primes, as described above.
Thus you can always find two or three primes that sum to any target n greater than 3.
Try this out!
Not an ideal code but if you want to have a working solution :P
primes = [2,3,5,7]
D = 29
i = -1
sum_ = 0
count = 0
while sum_ != D :
sum_ = sum_ + primes[i]
count += 1
if (sum_ == D) :
break
elif D - sum_ == primes[i-1] :
count += 1
break
elif D - sum_ < ex[i-1] and (D-sum_ not in primes) :
sum_ = sum_ - primes[i]
count = count - 1
i = i - 1
print(count)

Dynamic Programming solution for a Recursion solution

Given an input n , find the sum of all the possible combinations of numbers 1 ... n.
For example, if n=3 , then all the possible combinations are
(1),(2),(3),(1,2),(1,3),(2,3),(1,2,3)
and their sum is
1 + 2 + 3 + (1+2) + (1+3) + (2+3) + (1+2+3) =24
I am able to solve this problem using recursion. How can I solve this problem using Dynamic Programming ?
#include<iostream>
using namespace std;
int sum=0,n;
int f(int pos,int s)
{
if(pos>n)
{
return 0;
}
else
{
for(int i=pos+1;i<=n;++i)
{
sum+=s+i;
f(i,s+i);
}
}
}
int main()
{
cin>>n;
sum=0;
f(0,0);
cout<<sum<<'\n';
}
}
EDIT
Though this problem can be solved in constant time using this series.
But I want to know how this can be done using Dynamic Programming as I am very weak at it.
You do not need to use dynamic programming; you can use simple arithmetic if you want.
The number of cases is 2 ^ n, since each number is either on or off for a given sum.
Each number from 1 to n is used in exactly half of the sums, so each number comes 2 ^ (n-1) times.
1 + 2 + ... + n = (n - 1) * n / 2.
So the sum is (n - 1) * n / 2 * 2 ^ (n-1).
For n = 3, it is (4*3/2) * 4 = 24.
EDIT: if you really want to use dynamic programming, here's one way.
Dynamic programming makes use of saving the results of sub-problems to make the super problem faster to solve. In this question, the sub-problem would be the sum of all combinations from 1 ... n-1.
So create a mapping from n -> (number of combinations, sum of combinations).
Initialize with 1 -> (2,1). Because there are two combinations {0,1} and the sum is 1. Including 0 just makes the math a bit easier.
Then your iteration step is to use the mapping.
Let's say (n-1) -> (k,s), meaning there are k sets that sum to s for 1 ... n-1.
Then the number of sets for n is k * 2 (each combination either has n or does not).
And the sum of all combinations is s + (s + k * n), since you have the previous sum (where n is missing) plus the sum of all the combinations with n (which should be k * n more than s because there are k new combinations with n in each).
So add n -> (2*k,2*s + k*n).
And your final answer is the s in n -> (k,s).
let dp[n] be the result, Therefore:
dp[1] = 1
dp[n] = 2 * dp[n-1] + 2^(n-1) * n
First, it is obvious that dp[1] = 1
Second, dp[n] is the sum which contains n and sum which didn't contains n
E.G: dp[3] = {(1) (2) (1,2)} + {(3), (1,3), (2,3), (1,2,3)}
We can find dp[n-1] appear twice and the number of n appear 2^(n-1) times
I think maybe it is what you want.

how to check divisibility of a very long number in c++?

How to check divisibility of a very long number in c++?
i have to check if (3*n*n+3*n-1) is divisible by 2 , 3 and 5 and n can be upto 10^10 so long long int is also doubtful to work although i have tried doing this:
unsigned long long int gg4,l;
gg4=(3*n*n+3*n-1);
if(gg4%3==0){
gg4=gg4/3;
}
if(gg4%2==0){
gg4=gg4/2;
}
if(gg4%5==0){
gg4=gg4/2;
}
But i guess this wont work because of the range of n so please help! :)
after this i also need to divide (3*n*n+3*n-1) by that factor so please help!
To expand on my comment, there are two cases when (3*n*n+3*n-1) % 5 == 0, namely
n = 5 * m + 1
n = 5 * m + 3
For m an integer.
So in fact you don't need to calculate a long anything, you can work directly with n, and never even calculate 3 * n * n + 3 * n - 1 in the first place.
Just check:
n % 5 == 1 || n % 5 == 3

Recursive solution for Permutations

Hey I have a problem where I need to create two functions, countWithPerms() and ignorePerms() These two functions must be a recursive solution. countWithPerms() will count the number of actual permutations while ignorePerms() will only count the number of duplicate permutations.
So an example would be find the permutation for the number 3. So if I pass 3 into the function countWithPerms() would find that 3 = (2 + 1) = (1 + 2) = (1 + 1 + 1), so countWithPerms(3) is 3, because it counted 3 ways to sum up 3. While countIgnorePerms(3) is 2 because (1 + 2) and (2 + 1), would both not be counted in countWithPerms since they are the just written in the opposite order.
A large example would be countWithPerms(7) is 63, while countIgnorePerms(7) is 14.
I have countwithPerms done, but I am completely stuck on countIgnorePerms.
int countWithPerms( int n)
{
if(n == 1)
return 0;
else
n--;
return (countWithPerms(n) + 1) +
(countWithPerms(n));
}
int ignorePerms(int sum, int xmin){
if(sum == 1)
return 0;
else
for(int i=0; i<sum;i++){
sum += sum-xmin;
2*ignorePerms(sum,xmin)+1;
return sum;
}
}
The idea of counting without considering permutations is to consider only ordered solutions.
To do this pass in addition to n also what is the minimum value xmin that an addendum must have. For example
3 = 1 + 2
would be ok (because 2 >= 1), but
3 = 2 + 1
wouldn't be acceptable (because 1 < 2).
So the idea is to write a function that answers "how many sums with non-decreasing terms can give the prescribed total in the first addendum is not less than min_addendum?".
if min_addendum is bigger than total clearly the answer is 0
if total is 1 then there's only one sum
otherwise the first possible sum is total, then you should count as sums
min_addendum + a sum of other non-decreasing terms, the first not less than min_addendum totalling total-min_addendum
min_addendum+1 + a sum of other non-decreasing terms, the first not less than min_addendum+1 totalling total-min_addendum-1
min_addendum+2 + a sum of other non-decreasing terms, the first not less than min_addendum+2 totalling total-min_addendum-2
...

How can I find the number of ways a number can be expressed as a sum of primes? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Generating the partitions of a number
Prime number sum
The number 7 can be expressed in 5 ways as a sum of primes:
2 + 2 + 3
2 + 3 + 2
2 + 5
3 + 2 + 2
5 + 2
Make a program that calculates, in how many ways number n can be
expressed as a sum of primes. You can assume that n is a number
between 0-100. Your program should print the answer in less than a
second
Example 1:
Give number: 7 Result: 5
Example 2:
Give number: 20 Result: 732
Example 3:
Give number: 80 Result: 10343662267187
I've been at this problem for hours. I can't figure out how to get n from (n-1).
Here are the sums from the first 30 numbers by a tree search
0 0 0 1 2 2 5 6 10 16 19 35 45 72 105 152 231 332 500 732 1081 1604 2351 3493 5136 7595 11212 16534 24441
I thought I had something with finding the biggest chain 7 = 5+2 and somehow using the knowledge that five can be written as 5, 3+2, 2+3, but somehow I need to account for the duplicate 2+3+2 replacement.
Look up dynamic programming, specifically Wikipedia's page and the examples there for the fibonacci sequence, and think about how you might be able to adapt that to your problem here.
Okay so this is a complicated problem. you are asking how to write code for the Partition Function; I suggest that you read up on the partition function itself first. Next you should look at algorithms to calculate partitions. It is a complex subject here is a starting point ... Partition problem is [NP complete] --- This question has already been asked and answered here and that may also help you start with algorithms.
There're several options. Since you know the number is between 0-100, there is the obvious: cheat, simply make an array and fill in the numbers.
The other way would be a loop. You'd need all the primes under 100, because a number which is smaller than 100 can't be expressed using the sum of a prime which is larger than 100. Eg. 99 can't be expressed as the sum of 2 and any prime larger than 100.
What you also know is: the maximum length of the sum for even numbers is the number divided by 2. Since 2 is the smallest prime. For odd numbers the maximum length is (number - 1) / 2.
Eg.
8 = 2 + 2 + 2 + 2, thus length of the sum is 4
9 = 2 + 2 + 2 + 3, thus length of the sum is 4
If you want performance you could cheat in another way by using GPGPU, which would significantly increase performance.
Then they're is the shuffling method. If you know 7 = 2 + 2 + 3, you know 7 = 2 + 3 + 2. To do this you'd need a method of calculating the different possibilities of shuffling. You could store the combinations of possibilities or keep them in mind while writing your loop.
Here is a relative brute force method (in Java):
int[] primes = new int[]{/* fill with primes < 100 */};
int number = 7; //Normally determined by user
int maxLength = (number % 2 == 0) ? number / 2 : (number - 1) / 2; //If even number maxLength = number / 2, if odd, maxLength = (number - 1) / 2
int possibilities = 0;
for (int i = 1; i <= maxLength; i++){
int[][] numbers = new int[i][Math.pow(primes.length, i)]; //Create an array which will hold all combinations for this length
for (int j = 0; j < Math.pow(primes.length, i); j++){ //Loop through all the possibilities
int value = 0; //Value for calculating the numbers making up the sum
for (int k = 0; k < i; k++){
numbers[k][j] = primes[(j - value) % (Math.pow(primes.length, k))]; //Setting the numbers making up the sum
value += numbers[k][j]; //Increasing the value
}
}
for (int x = 0; x < primes.length; x++){
int sum = 0;
for (int y = 0; y < i; y++){
sum += numbers[y];
if (sum > number) break; //The sum is greater than what we're trying to reach, break we've gone too far
}
if (sum == number) possibilities++;
}
}
I understand this is complicated. I will try to use an analogy. Think of it as a combination lock. You know the maximum number of wheels, which you have to try, hence the "i" loop. Next you go through each possibility ("j" loop) then you set the individual numbers ("k" loop). The code in the "k" loop is used to go from the current possibility (value of j) to the actual numbers. After you entered all combinations for this amount of wheels, you calculate if any were correct and if so, you increase the number of possibilities.
I apologize in advance if I made any errors in the code.