How to obtain sum of all numbers with given digits? - software-design

Suppose I am given a number say,285. I have to find the sum of all possible combinations of the digits in the number. In this case we have 285 + 258 + 825 + 852 + 582 + 528.
I can use the itertools.permutations but it will take a lot of time to run. How do I solve this efficiently??

Related

How to find actual value of integers participated in sum operation

I have integers with power 2^n where n = 0 to 5
Lets say I have integers such as 2^0=1, 2^1=2, 2^2=4, 2^3=8, 2^4=16, 2^5=32
now when I get sum 42 of some integers belonging to above mentioned integer (the integers
cannot repeat itself i.e 2+2 cannot be done )
How can I obtain the actual integers participated in calculating the value 42 which is 32,8,2
Format your number as binary notation, in this case for 42 it would be 101010, from the notation get the index of 1 (start from 0) , it's 5,3,1. and then you have 2^5 + 2^3 + 2^1

Frequency Plot in R

I have an array of numbers (called tails.Z) ranging from 0 to 999 and I want to see which numbers appear most frequently. In order to do so, I do a simple frequency plot using hist(tails.Z, breaks=1000) with the following result:
According to the plot, the most frequent number appears over 400 times and is some value close to zero. A second peak is somewhere around the value of 200 and indicates that the number appears just short of 400 times.
However, when I do sort(table(tails.Z)) to see the actual numbers and their frequencies I get that the most frequent number is 175 which appears 377 times, then the 2nd most frequent number is 176 which appears 290 times, then 3 which appears 266 times, 0 255 times and 5 263 times. How is it possible that the first peak in the graph is higher than 400 but in table there is no number with that frequency?
EDIT: I should add that tails.Z is an array of integers ranging from 0 to 999 and that there is 114,411 elements in it.
See what hist function does using str:
hs=hist(tails.Z, breaks=1000)
str(hs)
tail(cbind(hs$mids,hs$counts),20)
barplot(hs$counts)
summary(hs$counts)

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

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.

Base 8 to hexadecimal conversion

I would just like to confirm the following answer.
7037108.
8 does not form part of the number. Converting this to hexadecimal I get the answer ABCDE. Is this correct? If so what is the purpose of the subscript / base 8 which is written slightly below the number 703710.
A very simple way of converting octal to hex is through binary: octal digits correspond to three binary digits, while hex digits correspond to four. Convert the number to binary, regroup by four-bit nibbles, and then convert to hex.
The number converted to binary looks like this:
111 000 011 111 001 000
7 0 3 7 1 0
Regroup by nibbles and add leading zeros for conversion to hex:
0011 1000 0111 1100 1000
Now you can easily convert the number using a lookup table.
The subscript indicates what base the number is already written in, in this case 8, so it's octal.
So 7037108 is saying that the number is 7 * 8^5 + 0 * 8^4 + 3 * 8^3 + 7 * 8^2 + 1 * 8^1 + 0 * 8^0, or 231368 in base 10.
IOW, 7037108 = 23136810 = 387C816.
ABCDE (by which you mean ABCDE16, or 0xABCDE), would be the right answer if the original number were written in base 10, because 70371010 = ABCDE16.

What is a canonical signed digit?

What is a canonical signed digit (CSD) and how does one convert a binary number to a CSD and a CSD back to a binary number? How do you know if a digit of a CSD should be canonically chosen to be +, -, or 0?
Signed-digit binary uses three symbols in each power-of-two position: -1, 0, 1. The value represented is the sum of the positional coefficients times the corresponding power of 2, just like binary, the difference being that some of the coefficients may be -1. A number can have multiple distinct representations in this system.
Canonical signed digit representation is the same, but subject to the constraint that no two consecutive digits are non-0. It works out that each number has a unique representation in CSD.
See slides 31 onwards in Parhi's Bit Level Arithmetic for more, including a binary to CSD conversion algorithm.
What is canonical signed digit format?
Canonical Signed Digit (CSD) is a type of number representation. The important characteristics of the CSD presentation are:
CSD presentation of a number consists of numbers 0, 1 and -1. [1, 2].
The CSD presentation of a number is unique [2].
The number of nonzero digits is minimal [2].
There cannot be two consecutive non-zero digits [2].
How to convert a number into its CSD presentation?
First, find the binary presentation of the number.
Example 1
Lets take for example a number 287, which is 1 0001 1111 in binary representation. (256 + 16 + 8 + 4 + 2 + 1 = 287)
1 0001 1111
Starting from the right (LSB), if you find more than non-zero elements (1 or -1) in a row, take all of them, plus the next zero. (if there is not zero at the left side of the MSB, create one there). We see that the first part of this number is
01 1111
Add 1 to the number (i.e. change the 0 to 1, and all the 1's to 0's), and force the rightmost digit to be -1.
01 1111 -> 10 000-1
You can check that the number is still the same: 16 + 8 + 4 + 2 + 1 = 31 = 32 + (-1).
Now the number looks like this
1 0010 000-1
Since there are no more consecutive non-zero digits, the conversion is complete. Thus, the CSD presentation for the number 287 is 1 0010 000-1, which is 256 + 31 - 1.
Example 2
How about a little more challenging example. Number 345. In binary, it is
1 0101 1001
Find the first place (starting from righ), where there are more than one non-zero numbers in a row. Take also the next zero. Add one to it, and force the rightmost digit to be -1.
1 0110 -1001
Now we just created another pair of ones, which has to be transformed. Take the 011, and add one to it (get 100), and force the last digit to be -1. (get 10-1). Now the number looks like this
1 10-10 -1001
Do the same thing again. This time, you will have to imagine a zero in the left side of the MSB.
10 -10-10 -1001
You can make sure that this is the right CSD presentation by observing that: 1) There are no consecutive non-zero digits. 2) The sum adds to 325 (512 - 128 - 32 - 8 + 1 = 345).
More formal definitions of this algorithm can be found in [2].
Motivation behind the CSD presentation
CSD might be used in some other applications, too, but this is the digital microelectronics perspective. It is often used in digital multiplication. [1, 2]. Digital multiplication consists of two phases: Computing partial products and summing up the partial product. Let's consider the multiplication of 1010 and 1011:
1010
x 1011
1010
1010
0000
+ 1010
= 1101110
As we can see, the number of non-zero partial products (the 1010's), which are has to be summed up depends on the number of non-zero digits in the multiplier. Thus, the computation time of the sum of the partial products depends on the number of non-zero digits in the multiplier. Therefore the digital multiplication using CSD converted numbers is faster than using conventional digital numbers. The CSD form contains 33% less non-zero digits than the binary presentation (on average). For example, a conventional double precision floating point multiplication might take 100.2 ns, but only 93.2 ns, when using the CSD presentation. [1]
And how about the negative ones, then. Are there actually three states (voltage levels) in the microcircuit? No, the partial products calculated with the negative sign are not summed right away. Instead, you add the 2's complement (i.e. the negative presentation) of these numbers to the final sum.
Sources:
[1] D. Harini Sharma, Addanki
Purna Ramesh: Floating point multiplier using Canonical Signed
Digit
[2] Gustavo A. Ruiz, Mercedes Grand: Efficient canonic signed digit recoding

Resources