How to find the number of ways a number is written with summation of one or more prime numbers? - primes

How to find the number of ways a number n is written with summation of one or more prime numbers?
For example:
n=5, the way is 2. 5,(2+3).
n=10, the way is 4. (2+2+2+2+2),(2+2+3+3),(2+3+5),(5+5).
1<=n<=1000

You might try searching for "prime partitions." Or you might be interested in this post at my blog. I won't give the code because you said you don't want it.

Related

How to fill a matrix with prime entries in C++?

I have this task:
Design a program which fills a matrix, of size n x n, with prime
entries (its entries must be prime numbers).
Now, I have a subroutine which reads and impries any matrix, when the user gives the entries of the matrix, and also have a subroutine which impries the prime numbers less than a given number of the user (as an array). What I can't do is try to combine these subroutines. Could you give me some good advices, please?
(I admit I misunderstood the question, as probably did some other commentators of the original post. It's relatively simple but not that trivial as it looks. For small inputs a naive approach 4. may work best.)
Let me reformulate the task:
Given a number N, find first N prime numbers.
Since you already implemented the sieve of Eratosthenes, the question is which number should be chosen as the upper limit for the sieve. Essentially, this is equivalent to finding the inverse of the prime counting function or to finding x, possibly smallest, such that
pi(x) >= N
(where pi is the prime counting function).
The article in wikipedia contains some hints, for example the inequality
pi(x) >= x/log(x).
So, one approach could rely on finding an approximate solution of the equation
x/log(x) = N,
which would be later used in the sieve of Eratosthenes. This can be done relatively easy (for small N even binary search will do).
There is, however, a widening gap between x/log(x) and pi(x) (see the table in the linked wikipedia article). So if we are really concerned about memory we could try a better inequality:
pi(x) >= li(x), (true for x <= 10^19)
where li is the logarithmic integral. This one gives a better approximation but a) we'd need some external library with the function 'li' and b) the inequality may not be true for very large x (probably not an issue here).
And if we'd like to improve the estimation even further (and for all x), we may need the assumption that the Riemann Hypothesis is true (yes, it's scary).
There are some direct algorithms for calculating pi but it's not worth using them for this task.
More direct approach:
make a guess for the upper limit in the sieve, say A, and run the sieve
if number of primes is too small, choose a larger upper limit, say B, and run the sieve, starting with the primes already found, for numbers in interval (A,B]; repeat.
If in 4. you are off by very few primes, a brute force may be faster. I've just found this post with interesting answers.

Factorize integer larger than 200 digits c++

I have to factorize a number given by text in C++, this number is greater than a double, and I can't use external libraries. I know this number is a product of the first 25 prime numbers. Is there a way I can process this number and give it's descomposition in prime?
Whats the best algorithim I could use to do so?

Primes in N (estimation)

I would like to know if there is any way (like a formula) to estimate how many prime numbers are there in an interval [0,N]. i.e. "How many prime numbers there are up to 120?"
I don't want to count and I dont want to know which numbers are these. I just need a estimation of how many are them.
Thank you.
f(x) = number of primes less than x
f(x) can be approximated by x/logx.
There are better but more complicated approximations, but a function for calculating this exactly is not known yet.

Possible combination to form a given number

How can I check that a given number can be formed by the positive integral combination of a given list of numbers.
For example, if the list of number is,
5 3 9
and
13
Then 13 can be formed by, 5*2 + 3. What is the possible algo for this? This is not a HW question. This was asked in an interview which I am preparing for. Please help!
I did this decades ago for combos of six numbers, (Countdown numbers game). If the set of numbers is in a global array, all you need to pass down through each recursion is one integer index that describes how far along the array you have examined so far.

countdown from large number

I'm working on a solution to the third exercise of project Euler, and I need to loop over the odd numbers below sqrt(600851475143.0). But I can't subtract 2 from the number every time the loop iterates, it stays the same every time. According to this answer that is due to how numbers are stored and that everything just above and everything under the decimal point is lost. How do I solve this? I need decimal numbers, so I can't use an int (which would not have been big enough anyway).
Since you're looking for odd numbers, and odd numbers are by definition integer, just use an appropriate integer type instead of floating-point maths.