Project Euler #23 - What does this mean? [closed] - math

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
http://projecteuler.net/problem=23
I am not looking for an answer . but can somebody explain me what does this means ?
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the
smallest number that can be written as the sum of two abundant numbers
is 24.
if 12 is smallest abundant number , how come 24 is smallest abundant number that can be written as sum of 2 abundant numbers ?
Problem Text
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

Let n be a number.
If the (sum of proper divisors of n) equals n, then n is perfect.
For example, 6 is perfect, because 1 + 2 + 3 = 6.
If the (sum of proper divisors of n) is less than n, then n is deficient.
For example, 5 is deficient, because 1 < 5.
If the (sum of proper divisors of n) is greater than n, then n is abundant.
As said in the text, for example 12 is abundant, because 1 + 2 + 3 + 4 + 6 > 12.
That said, if 12 is the smallest abundant number and we need to find an abundant number that is a sum of two abundant numbers, the smallest one we can check is the sum of twice the minimal one!
We need A and B such that A + B = C where A, B, C are abundant.
12 is the minimal abundant number and can be both A and B. There's nowhere said that the numbers have to be different. The definitions in Project Euler are pretty well worded, don't assume things that aren't said unless you can prove them. This is math, not a trick question.
Therefore, since A and B can be both 12, the smallest number to look at is 12 + 12 = 24. Which is abundant.

Related

Check whether a number can be expressed as sum of x powers of two

Is there a bit trick to check whether a number can be expressed as sum of x powers of 2?
Example: For x=3 n=21, the numbers are 16, 4, and 1. If n=30, it should be false, because there are no 3 powers of two to represent 30.
For a number n …
… the minimum x is the number of 1-bits in n. This number is called popcount(n).
Example: The binary number 0b1011 needs at least popcount(0b1011)=3 powers of two to be summed up (0b1000+0b0010+0b0001).
… the maximum x is n. Because 1 is a power of two you can add 1 n times to get n.
Now comes the hard question. What if x is between popcount(n) and n?
As it turns out, all of these x are possible. To build a sum of x powers of two …
start at the shortest sum (the binary representation of n)
If you have less than x addends, split any addend that is bigger than 1 into two addends, increasing the number of addends by one. This can be done until you arrive at x=n.
Example: Can 11=0b1011 be expressed as a sum of x=7 powers of two?
Yes, because popcount(n)=3 <= x=7 <= n=11.
To build a sum with x=7 powers of two we use
11 = 0b1011 = 0b1000+0b10+0b1 | only 3 addends, so split one
= (0b100+0b100)+0b10+0b1 | only 4 addends, so split another one
= ((0b10+0b10)+0b100)+0b10+0b1 | only 5 addends, so split another one
= (((0b1+0b1)+0b10)+0b100)+0b10+0b1 | only 6 addends, so split another one
= (((0b1+0b1)+(0b1+0b1))+0b100)+0b10+0b1 | 7 addends, done
Implementation
To implement the check »can n can be expressed as sum of x powers of two?« use
isSumOfXPowersOfTwo(n, x) {
return x<=n && x>=popcount(n).
}
For efficient bit-twiddling implementations of popcount see this question. Some processors even have an instruction for that.

Give a big-O bound on the number of bits needed to represent a number N

N is random number,
I am confused with bound.
Any help is appreciated.
Well, for a random number n, there exists a, b such that 2^a <= n <= 2^b or just a k such that 2^(k-1) <= n <= 2^k - 1 (1). We know that for any number less than 2^n, we need log(2^n) = n * log(2) = n bits to represent it (2). For example:
5: 4 < 5 < 8; we need 3 bits for 4, 5 bits for 8 => we need 4 bits for 5
23: 16 = 2^4 < 23 < 32 = 2^5; so we need 5 bits to represent 23
In conclusion, for an exact number of bits b for a random number n, we can use the formula:
b = floor(log(n)) + 1
So, the big-O notation that we are gonna use is O(floor(log(n)) + 1) = O(logn).
Extra info:
SO Answer
Article
1) I supposed it is a random, integer, positive number (although it's easy to generalize for negative numbers too) which I suppose it is your problem case; for fractional numbers is's a bit harder to generalize this formulae
2) The log notation refers to logarithm in base 2

Is there a closed form available for the following table?

Below is a table which has a recursive relation as current cell value is the sum of the upper and left cell.
I want to find the odd positions for any given row denoted by v(x) as represented in the first column.
Currently, I am maintaining two one arrays which I update with new sum values and literally checking if each positions value is odd or even.
Is there a closed form that exists which would allow me to directly say what are the odd positions available (say, for the 4th row, in which case it should tell me that p1 and p4 are the odd places).
Since it is following a particular pattern I feel very certain that a closed form should exist which would mathematically tell me the positions rather than calculating each value and checking it.
The numbers that you're looking at are the numbers in Pascal's triangle, just rotated ninety degrees. You more typically see it written out like this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
...
You're cutting Pascal's triangle along diagonal stripes going down the left (or right, depending on your perspective) strips, and the question you're asking is how to find the positions of the odd numbers in each stripe.
There's a mathematical result called Lucas's theorem which is useful for determining whether a given entry in Pascal's triangle is even or odd. The entry in row m, column n of Pascal's triangle is given by (m choose n), and Lucas's theorem says that (m choose n) mod 2 (1 if the number is odd, 0 otherwise) can be found by comparing the bits of m and n. If n has a bit that's set in a position where m doesn't have that bit set, then (m choose n) is even. Otherwise, (m choose n) is odd.
As an example, let's try (5 choose 3). The bits in 5 are 101. The bits in 3 are 011. Since the 2's bit of 3 is set and the 2's bit of 5 is not set, the quantity (5 choose 3) should be even. And since (5 choose 3) = 10, we see that this is indeed the case!
In pseudocode using relational operators, you essentially want the following:
if ((~m & n) != 0) {
// Row m, entry n is even
} else {
// Row m, entry n is odd.
}

if two n bit numbers are multiplied then the result will be maximum how bit long

I was wondering if there is any formula or way to find out much maximum bits will be required if two n bits binary number are multiplied.
I searched a lot for this but was unable to find its answer anywhere.
Number of digits in base B required for representing a number N is floor(log_B(N)+1). Logarithm has this nice property that log(X*Y)=log(X)+log(Y), which hints that the number of digits for X*Y is roughly the sum of the number of digits representing X and Y.
It can be simply concluded using examples:
11*11(since 11 is the maximum 2 bit number)=1001(4 bit)
111*111=110001(6 bit)
1111*1111=11100001(8 bit)
11111*11111=1111000001(10 bit)
and so from above we can see your answer is 2*n
The easiest way to think about this is to consider the maximum of the product, which is attained when we use the maximum of the two multiplicands.
If value x is an n-bit number, it is at most 2^n - 1. Think about this, that 2^n requires a one followed by n zeroes.
Thus the largest possible product of two n-bit numbers will be:
(2^n - 1)^2 = 2^(2n) - 2^(n+1) + 1
Now n=1 is something of a special case, since 1*1 = 1 is again a one-bit number. But in general we see that the maximum product is a 2n-bit number, whenever n > 1. E.g. if n=3, the maximum multiplicand is x=7 and the square 49 is a six-bit number.
It's worth noting that the base of the positional system doesn't matter. Whatever formula you come up with for the decimal multiplication will work for the binary multiplication.
Let's apply a bit of deduction and multiply two numbers that have relatively prime numbers of digits: 2 and 3 digits, respectively.
Smallest possible numbers:
10 * 100 = 1000 has 4 digits
Largest possible numbers:
99 * 999 = 98901 has 5 digits
So, for a multiplication of n-digit by m-digit number, we deduce that the upper and lower limits are n+m and n+m-1 digits, respectively. Let's make sure it holds for binary as well:
10 * 100 = 1000 has 4 digits
11 * 111 = 10101 has 5 digits
So, it does hold for binary, and we can expect it to hold for any base.
x has n binary digits means that 2^(n-1) <= x < 2^n, also assume that y has m binary digits. That means:
2^(m+n-2)<=x*y<2^(m+n)
So x*y can have m+n-1 or m+n digits. It easy to construct examples where both cases are possible:
m+n-1: 2*2 has 3 binary digits (m = n = 2)
m+n: 7*7=49=(binary)110001 has 6 binary digits and m = n = 3

Number Theory - Factors, HCF and LCM [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
30, 40 and 'n' are such that every number is a factor of the product of other 2 number. If 'n' is a positive
integer , what is the difference between the maximum value of 'n' and the minimum value of 'n'?
Now, since it says that n is a factor of the product of the other 2 numbers, the max value that n can take is 1200 right?
i guess the hcf will give the minimum value of n
Listing the factors of 30 and 40
30 -> 1,2,3,5,6,10,15,30
40 -> 1,2,4,5,8,10,20,40
hcf(30,40) -> 10
Therfore, the difference is 1200-10 => 1190..
But the answer that is given is 1188...where am i going wrong?
Your approach is wrong. The greatest common divisor of 30 and 40 is not your smallest n.
You are looking for the smallest integer n > 0 that satisfies 40*n = 0 (mod 30) and 30*n = 0 (mod 40).
For the first equation, the result is n_1 = 3. For the second equation, we get n_2 = 4. The smallest n to satisfy both equations is the least common multiple of n_1 and n_2 -- in this case, n = 12.
hcf(30,40) -> 12
30=2*3*5
40=2*2*2*5
So, hcf(30,40) -> 3*2*2=12

Resources