Euler 004 - Excel. Why is this array not finding the largest palidrome? - arrays

=MAX(B2:AHQ901*--AND(LEFT(B2:AHQ901,1)=RIGHT(B2:AHQ901,1),MID(B2:AHQ901,2,1)=MID(B2:AHQ901,5,1),MID(B2:AHQ901,3,1)=MID(B2:AHQ901,4,1)))
Attempting Euler challenge 004 in Excel 2013.
I have set up a simple spreadsheet that sums all 3 digit numbers.
The above array should find the maximum number in this spreadsheet that is a palidrome, but returns #value.
Any suggestions?

If that formula returns #VALUE! as an array formula then somewhere in B2:AHQ901 is a non numeric value.
But there is also an issue with the using of AND in array context. The AND will be evaluated first with the whole array. It will not be evaluated for each array element. So the whole AND will be false if only one comparison is false in the whole array.
It should be:
{=MAX(IF(ISNUMBER(B2:AHQ901),B2:AHQ901)*
(LEFT(B2:AHQ901,1)=RIGHT(B2:AHQ901,1))*
(MID(B2:AHQ901,2,1)=MID(B2:AHQ901,5,1))*
(MID(B2:AHQ901,3,1)=MID(B2:AHQ901,4,1)))}

Related

Value output by formula - not sure why array formula EXCEL

Using the array formula below {=MMULT(MMULT(C27:D27,(H27:I28)),TRANSPOSE(C27:D27))} I get 35.05425707.
Using the below array {=(MMULT(C27:D27,(H27:I28)))} I get 5.763564268.
Although I understand perfectly how the second formula output the value of 5.763564268 I do not understand how the 35.05425707 was output. Can I ask for a step by step breakdown? Please find a view of the file below:
Thank you in advance.
MMULT is an array formula and the first MMULT actually results in an array of 2 values:
=MMULT(B7:C7,G7:H8) returns an array of 5.7636 & 5.8818
When you do the second MMULT:
=MMULT(MMULT(B7:C7,G7:H8),TRANSPOSE(B7:C7))
You multiply 5.7636 by 2 and 5.8818 by 4. Added up that results in 35.0544.*
*(I used the screenshot for reference and the numbers are not matching, since the values shown are rounded values)

Excel: creating an array with n times a constant

I have been looking around for a while but unable to find an answer to my question.
In Excel, what compact formula can I use to create an array made up of a single element repeated n times, where n is an input (potentially hard-coded)?
For example, something that would look like this (the formula below does not work but gives an idea of what I am looking for):
{={"Constant"}*3}
Note: I am not looking for a VBA-based solution.
EDIT Reading #AxelRichter answer, I see I should also indicate that the formulas below assume Constant is a number. If Constant is text, then this solution will not work.
Volatile:
=ROW(INDIRECT("1:" & Repts))/ROW(INDIRECT("1" & ":" & Repts)) * Constant
non-Volatile:
=ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,Repts,1))/ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,Repts,1))*Constant
If
Constant = 14
Repts = 3
then
Result = {14;14;14}
The first part of the formulas create an array of 1's repeated Repts times. Then we multiply that array by Constant to get the desired result.
And after reading #MacroMarc's comment, the following non-volatile formula shouyld also work for numbers:
=(ROW($A$1:INDEX($A:$A,Repts))>0)*Constant
One could concatenate 1:n empty cells to the "Constant" to create a string array having n items "Constant":
"Constant"&INDEX(XFD:XFD,1):INDEX(XFD:XFD,3)
There 3 is n.
Used in Formula
=INDEX("Constant"&INDEX(XFD:XFD,1):INDEX(XFD:XFD,3),0)
Evaluate Formula shows that it works:
Here column XFD is used because in most cases this column will be empty and a column which is guaranteed to be empty is needed for this solution.
If used
"Constant"&T(ROW($A$1:INDEX($A:$A,3)))
=INDEX("Constant"&T(ROW($A$1:INDEX($A:$A,3))),0)
the need of an empty column disappears. The function ROW returns numbers but the T returns an empty string if its parameter is not text. So empty strings will be concatenated for each 1:3 (n).
Thanks to #MacroMarc for the hint.
Try:
REPT("Constant", SEQUENCE(3,1,1,0))
Or, if the reference is to a dynamic array:
REPT("Constant", SEQUENCE(A1#,1,1,0))
The dynamic array spills, and has your constant repeated one time.
Using SEQUENCE with a step of 0 is a much cleaner way to make an array of constants. You can choose whether you want rows or columns (or both!) as well.
=SEQUENCE(Repts,1,Constant,0)
I will generally use a sequence (like Claire (above) said). But if you want to provide an output of text objects, I would do it this way:
=IF(SEQUENCE(A1,A2,1,0),A3)
Where:
A1 has the number of rows
A2 has the number of columns
A3 has the thing you want repeated into an array
The sequence will create a matrix of 1's, which the IF statement will default to the TRUE expression (being the contents of A3).
So, if you wanted a vertical list of 3 items that says "Constant", this would do it:
=IF(SEQUENCE(3,,1,0),"Constant")
If you would prefer it be arranged horizontally instead of vertically, just amend the SEQUENCE function:
=IF(SEQUENCE(,3,1,0),"Constant")

Excel COUNT() vs COUNTIF() with arrays

I think this should be a straightforward question, but for some reason I can't find a solution anywhere.
I have a lengthy formula in excel that, ultimately, returns an array of four items -- i.e. {1,2,0,0}. I want to count how many of the resulting numbers are greater than zero.
When I use =COUNT({1,2,0,0}) on this result, I get the expected answer of 4. But when I try to use =COUNTIF({1,2,0,0}, ">0") it pops up with an error saying that my formula has a problem.
Is there something I'm doing wrong? Is there an array equivalent for COUNTIF() ?
It appears the COUNTIF function only works on ranges, while the COUNT function can utilize an array.
Give SUMPRODUCT a try. Below is a slightly expanded form of your example which I used to test the formula. It basically checks to see if each value in the array is greater than 0, and if it is, it assigns it a value of 1. Then SUMPRODUCT goes through and adds up all the 1s to give you the total number of values greater than 0.
=SUMPRODUCT(IF({1,0,3,0,5,0,0,6,9,9,0,7,0}>0,1,0))
Probably the most concise way to accomplish this is to just convert the TRUE or FALSE value returned from the validation check into a number with the INT function. TRUE translates to 1 and FALSE translates to 0. Then SUM those 1's and 0's.
=SUM(INT({1,2,0,0}>0))
Or as Barry Houdini points out, you can coerce the boolean to an int with:
=SUM(({1,2,0,0}>0)*1)
Or:
=SUM(({1,2,0,0}>0)+0)

Excel Array Formula Error

I am coming to seek guidance with another question with Excel Array Formulas.
I am using Excel 2003 and trying to understand a new spreadsheet with the following type of formula :
{=IF($B$6,SUM(($C$6:$AM$6=1)*1),)}
I have tried using the Excel formula audit tool to understand the formula but it crashes when I run it on these type of array formula.
As of now I am thinking that the formula does this :
B6 has a number = True / False
If True do SUM of (C6:AM6=1) and multiply by 1
If False do nothing
However, I am not 100% of the second statement. Does it say SUM the number of times 1 is present in C6:M6 then multiply by 1? If so why multiply by 1. My only guess on the latter is that the (C6:M6=1) returns a True or False value and the *1 converts it to 0 or 1. Then if this is correct, what is the purpose of the sum function?
Thanks for any guidance.
You are correct.
($C$6:$AM$6=1) returns an array of TRUE/FALSE values, which SUM would ignore.
Multiplying by 1 creates an array of 1/0 values, which are then added by SUM to create a count of the number of cells in the range that equal 1.
COUNTIF would be simpler as mentioned previously.
I think you do not need an array function. Try this non-array formula:
=IF($B$6,SUMIF($C$6:$AM$6,1,$C$6:$AM$6),"Do Nothing")

How to use arithmetic operators to get cell location

I'm trying to perform math within an Excel formula to specify the end of an array. Here is a simple version of what I want to do:
A B C
1 =COUNTA(A1:A5)-1 =SUM(A1:A(1+B1))
2
3
4
5
The first column is my data array. The second column counts the number of entries in that array (so if it isn't full, it returns a value less than 5). The third column sums the array starting with the first value and ending with the last entered value. Obviously with the SUM function it doesn't matter if there are zeroes, but I am trying to use the MATCH function and I don't want it to return a zero just because there are blank entries in the array I'm looking up.
So I want to modify the information within the SUM function to produce a variable array length based on the return value of B1. I hope this is clear. Thanks for any help!
One way is to use INDEX like this
=SUM(A1:INDEX(A:A,B1+1))
you can use the range defined by
A1:INDEX(A:A,B1+1)
in other functions like MATCH
Like this?
=SUM(INDIRECT("A1:A"&(1+B1)))

Resources