Counting binary digits in a list of excel cells - excel

I'm trying to make a formula that transforms a list decimal numbers to binary, then counts the number of appearances of ones at a certain position. I was trying to build an array formula that went something like this:
{=SUM(MID(DEC2BIN(A1:A10;10);9;1)}
This will return #VALUE. Is there a way to do this?
EDIT: examples added
Input (Binary Equivalent)
2 0000000010
3 0000000101
7 0000000111
7 0000000111
5 0000000101
9 0000001001
Outputs Result
(digit to sum
from the right)
1 5
2 3
3 3
4 1

This was another way e.g. for the second digit from the right
=SUMPRODUCT(--ISODD(A1:A10/2))
Divide by 2^(n-1) where n is digit numbered from the right: the ISODD function ignores any fraction that results from the division.

=SUM(0+MID(DEC2BIN(--A1:A10,10),9,1))
array-entered.
Regards

If you are trying to count have many of the second digits are set in a range of numbers you can do this:
={SUM((MOD(A1:A10,4)>=2)+0)}
To understand this, let's look at some example data
Here I have some decimal numbers with their binary equivalents. In column C I have just extracted the 2nd digit (i.e. your MID(A1,9,1)). Then in column D I just take the modulo by 4. You can see that when the remainder is greater than 2, the second digit is set.
MOD(A1,4) basically divides the number by 4 and gives us the remainder (the numerator of the remainder if it was represented as a fraction over 4). With binary numbers, division by a power of two is just a right shift. Division by 4 is a right shift by 2 and the numbers that 'fall off' are the remainder. In this case it's the first two digits. They can be
00 | 0
01 | 1
10 | 2
11 | 3
so we see that the second digit is set only when the remainder is greater than 2.
Note the +0 in the original formula is to cast the boolean result of = to an integer so we can use SUM i.e. SUM({TRUE,FALSE}) doesn't work but SUM({TRUE,FALSE}+0) computes to SUM({1,0}) which does work.
To make this generic, let's assume you want to do it for the $E$1th digit:
=SUM((MOD(A1:A12,2^$E$1)>=2^($E$1-1))+0)

With bit operations it's not necessary to treat the number as a string.
{=SUM(BITAND(A1:A10;2^(C1-1))/2^(C1-1))}
Assuming the position you are looking for is stored in C1.

Related

TRUE/FALSE ← VLOOKUP ← Identify the ROW! of the first negative value within a column

Firstly, we have an array of predetermined factors, ie. V-Z;
their attributes are 3, the first two (•xM) multiplied giving the 3rd.
f ... factors
• ... cap, the values in the data set may increase max
m ... fixed multiplier
p ... let's call it power
This is a separate, standalone array .. we'd access with eg. VLOOKUP
f • m pwr
V 1 9 9
W 2 8 16
X 3 7 21
Y 4 6 24
Z 5 5 25
—————————————————————————————————————————————
Then we have 6 columns, in which the actual data to be processed is in, & thereof derive the next-level result, based on the interaction of both samples introduced.
In addition, there are added two columns, for balance & profit.
Here's a short, 6-row data sample:
f • m bal profit
V 2 3 377 1
Y 2 3 156 7
Y 1 1 122 0
X 1 2 -27 2
Z 3 3 223 3
—————————————————————————————————————————————
Ultimately, starting at the end, we are comparing IF -27 inverted → so 27 is within the X's power range ie. 21 (as per the first sample) .. which is then fed into a bigger formula, beyond the scope of this post.
This can be done with VLOOKUP, all fine by now.
—————————————————————————————————————————————
To get to that .. for the working example, we are focusing coincidentally on row5, since that's the one with the first negative value in the 'balance' column, so ..
on factorX = which factor exactly is to us unknown &
balance -27 = which we have to locate amongst potentially dozens to hundreds of rows.
Why!?
Once we know that the factor is X, based on the * & multiplier pertaining to it, then we also know which 'power' (top array) to compare -27, as the identified first negative value in the balance column, to.
Is that clear?
I'd like to know the formula on how to achieve that, & (get to) move on with the broader-scope work.
—————————————————————————————————————————————
The main issue for me is not knowing how to identify the first negative or row -27 pertains to, then having that piece of information how to leverage it to get the X or identify the factor type, especially since its positioned left of the latter & to the best of my knowledge I cannot use negative column index number (so, latter even if possible is out of the question anyway).
To recap;
IF(21>27) = IF(-21<-27)
27 → LOCATE ROW with the first negative number (-27)
21 → IDENTIFY the FACTOR TYPE, same row as (-27)
→ VLOOKUP pwr, based on factor type identified (top array, 4th column right)
→ invert either 21 to a negative number or (-27) to the positive number
= TRUE/FALSE
Guessing your columns I'll say your first chart is in columns A to D, and the second in columns G to K
You could find the letter of that factor with something like this:
=INDEX(G:G,XMATCH(TRUE,INDEX(J:J<0)))
INDEX(J:J<0) converts that column to TRUE and FALSE depending on being negative or not and with XMATCH you find the first TRUE. You could then use that in VLOOKUP:
=VLOOKUP(INDEX(G:G,XMATCH(TRUE,INDEX(J:J<0))),A:D,4,0)
That would return the 21. You can use the first concept too to find the the -27 and with ABS have its "positive value"
=VLOOKUP(INDEX(G:G,XMATCH(TRUE,INDEX(J:J<0))),A:D,4,0) > INDEX(J:J,XMATCH(TRUE,INDEX(J:J<0)))
That should return true or false in the comparison

Product MarkUp with Excel Formula

I have a list of product price that i need to markup.
I've tried using with Regular Formula
=A1*10%+A1+5,000 = 36,130 (A1 = 28,300)
Is there any way to remove the last 2 Digit and adjust the price if the price result is 36,130 it will remove the 30 (36,100) but if the price 36,160 it will adjust to 36,200 ?
Use:
=MROUND(<YourExpression>,100)
In order to round up to a certain number of digits, this is what I usually do (example of 2 digits):
multiply by 100
round to the nearest integer
divide by 100 again
(use 1000 for 3 digits, ...)
For rounding, you might use the ROUND() worksheet function.

How many operations can we do with an 8-digit (plus decimal) calculator?

I have this model: a simple 8-digit display calculator (no memory buttons, no square root etc etc) has buttons (the decimal point does not count as a 'digit'):
10 buttons for integers 0 to 9,
1 button for dot (decimal point, so it can hold decimals, like from 0.0000001 to 9999999.9),
4 buttons for operations (+, -, /, *), and
1 button for equality (=). (the on/off button doesn't count for this question)
The question is two-fold: how many numbers can they be represented on the calculator's screen? (a math-explained solution would be appreciated)
*AND
if we have to make all 4 basic operations between any pair of 2 numbers, of the above calculated, how many operations would that be?
Thank you for your insight and help!
For part one of this answer, we want to know how many numbers can be represented on the calculator's screen.
Start with a simplified example and work up from there. Let's start with a 1-digit display. With this calculator, you can display the numbers from 0 to 9, and you can display each of those numbers with a decimal point either before the digit (making it a decimal), or after the digit (making it an integer). How many unique numbers can be made?
.0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.
That's 20 possibilities with 1 repeat number makes 19 unique numbers. Let's find this result again, but using a mathematical approach that we can scale up to a larger number of digits.
Start by finding all the numbers 0 <= n < 1 that can be made. For the numbers to fit in that range, the decimal point must be before the first digit. We're still dealing with 1 digit, so there are 101 different ways to fill the calculator with numbers that are greater than or equal to 0, but less than 1.
Next, find all the numbers 1 <= n < 10 that can be made. To do this, you move the decimal point one place to the right, so now it's after the first digit, and you also can't allow the first digit to be zero (or the number will be less than 1). That leaves you 9 unique numbers.
[0<=n<1] + [1<=n<10] = 10 + 9 = 19
Now we have a scaleable system. Let's do it with 2 digits so you see how it works with multiple digits before we go to 8 digits. With 2 digits, we can represent 0-99, and the decimal point can go in three different places, which means we have three ranges to check: 0<=n<1, 1<=n<10, 10<=n<100. The first set can have zero in its first place, since zero is in the set, but every other set can't have zero in the first place or else the number would be in the set below it. So the first set has 102 possibilities, but each of the other sets has 9 * 101 possibilities. We can generalize this by saying that for any number d of digits that our calculator can hold, the set 0<=n<1 will have 10d possibilities, and each other set will have 9 * 10d-1 possibilities
So for 2 digits:
[0<=n<1] + [1<=n<10] + [10<=n<100] = 100 + 90 + 90 = 280
Now you can see a pattern emerging, which can be generalize to give us the total amount of unique numbers that can be displayed on a calculator with d digits:
Unique displayable numbers = 10d + d * 9 * 10d-1
You can confirm this math with a simple Python script that manually finds all the unique numbers that can be displayed, prints the quantity it found, then also prints the result of the formula above. It gets bogged down when it gets to higher numbers of digits, but digits 1 through 5 should be enough to show the formula works.
for digits in range(1, 6):
print('---%d Digits----' % digits)
numbers = set()
for d in range(digits + 1):
numbers.update(i / 10**d for i in range(10**digits))
print(len(set(numbers)))
print(10**digits + digits * 9 * 10**(digits - 1))
And the result:
---1 Digits----
19
19
---2 Digits----
280
280
---3 Digits----
3700
3700
---4 Digits----
46000
46000
---5 Digits----
550000
550000
Which means that a calculator with an 8 digit display can show 820,000,000 unique numbers.
For part two of this answer, we want to know if we have to make all 4 basic operations between any pair of 2 numbers, of the above calculated, how many operations would that be?
How many pairs of numbers can we make between 820 million unique numbers? 820 million squared. That's 672,400,000,000,000,000 = 672.4 quadrillion. Four different operations can be used on these number pairs, so multiply that by 4 and you get 2,689,600,000,000,000,000 = 2.6896 quintillion different possible operations on a simple 8 digit calculator.
EDIT:
If the intention of the original question was for a decimal point to not be allowed to come before the first digit (a decimal 0<=n<1 would have to start with 0.) then the formula for displayable numbers changes to 10d + (d - 1) * 9 * 10d-1, which means the amount of unique displayable numbers is 730 million and the total number of operations is 2.1316 quintillion.

Removing ".00" from Decimal format numbers to make them whole numbers keeping other decimal numbers intact

I have a column that has Amount rounded to 2 decimal places. So there are two types of enteries, one of the form 359.00 and others 359.78. I want to remove .00 from the first form of enteries to make it interger(359) keeping the decimal format of other number intact.
Data are floating numbers
A column don't have mixed types. Assuming your data are stored as floating numbers (dtype: float64), they will remain floats. What you can do is to use a custom format so that they are shown on the screen as you wish, but internally they remains floats. For example:
dfa = pd.DataFrame.from_records([(1,), (3.34,), (2.49,), (5,), (7,)], columns=['Amount'])
pd.options.display.float_format = lambda x : "{:2.2f}".format(x).rstrip('0').rstrip('.')
print(dfa)
This prints:
Amount
0 1
1 3.34
2 2.49
3 5
4 7
Data are strings
If instead your data are just strings representing numbers (dtype: object) you can use pandas.applymap to actually edit the strings according to a format style.
dfb = pd.DataFrame.from_records([("1.00",), ("3.34",), ("2.49",), ("5.00",), ("7.00",)], columns=['Amount'])
dfbb = dfb.applymap(lambda x : str(x).rstrip('0').rstrip('.'))
print(dfbb)
This prints (again):
Amount
0 1
1 3.34
2 2.49
3 5
4 7
Data are floating numbers but you want strings
In this case you can combine the two methods:
dfc = dfa.applymap(lambda x : "{:2.2f}".format(x).rstrip('0').rstrip('.'))
print(dfc)
Starting from a dataframe with floating numbers, you end with a dataframe of formatted strings. It prints the same, no need to touch pandas format settings.
Credits to this answer for the basic idea.

IF THEN Statement Multiple Conditions

I'm trying to design an excel formula for some golf game scores. Golfers get points based on a random number (0-9) and the last digit of their score. So, if the random number is 0 and the golfers score ends in 0, they get 10 points. Still with a 0 random number, if the golfers score ends in a 1, they get 9 points. 8 points for a last digit of 2. 7 for 3. 6 for 4. 10 for 5. 9 for 6. 8 for 7. 7 for 8. 6 for 9.
Score ends in: Points:
0 10
1 9
2 8
3 7
4 6
5 10
6 9
7 8
8 7
9 6
As long as I come up with one formula for the random number of 0, I can adjust it for the remaining 9 random numbers.
The way I was hoping for this to work was to just be able to enter the scores into one column and then have the points calculated in a separate column. There will also have to be a cell where I enter the random number.
Any help is appreciated!
You can use something like this to get the score if the random number is 0:
=IF(A1=0,10-MOD(RIGHT(B1),5))
This will give the points you mentioned provided:
A1 is the cell containing the random number
B1 is the cell containing the points of the golfer.
The main formula here is:
10-MOD(RIGHT(B1),5)
RIGHT() takes the last digit of the points. MOD(,5) will get the remainder when this digit is divided by 5.
When you have 0, you get no remainder, hence 0.
When you have 1, you get a remainder of 1, hence 1.
When you have 2, you get a remainder of 2, hence 2.
When you have 6, you get a remainder of 1, hence 1 again.
Then 10 minus that remainder gives you the points you're looking for.
You could certainly use =RAND()*10to get a random value in excel. Or to get a value without commas use = ROUNDDOWN(RAND()*10;0)
Then you add something a VLOOKUPto get a value to each players score. RIGHT(A1;1)gives you the last value of a field.
VLOOKUPrequires you to have a table somewhere with the values for each score as you described.
edit: the MOD solution looks even better. Please note that RANDOM gets a fresh value everytime you refresh the XLS sheet. so probably use it to get a value and put that into another field manually.

Resources