3

I'm trying to generate a list of all the factors of a number in MS Excel. I have the following formula:

=NOT(MOD(12,SEQUENCE(1,12)))*(SEQUENCE(1,12))

This gives me an array with factors and zeros, as below:

1 2 3 4 0 6 0 0 0 0 0 12

But I can't seem to find a formula to remove the zeros. I've tried different permutations of LET, LAMBDA, LARGE, FILTER, SORT, and others. Nothing works. Any tips on what I could be doing wrong? I want to do this without VBA.

3
  • 5
    you have two answers, so I will not answer, but building on theirs, use LET to do the formula once: =LET(x,NOT(MOD(12,SEQUENCE(1,12)))*(SEQUENCE(1,12)),FILTER(x,x<>0)) Commented Oct 15 at 17:54
  • 10
    @ScottCraner the last part could also be FILTER(x,x) because any value other than 0 is considered TRUE and 0 is FALSE Commented Oct 15 at 19:19
  • 2
    @P.b Yes, good catch. Commented Oct 15 at 19:23

4 Answers 4

3

This lists numbers from 1 to 12, checks which divide 12 evenly, and filters out the rest. The formula repeats the expression to test which of those numbers are not zero; that’s how FILTER() knows which entries to include in the final list. Will work in your scenario.


=FILTER(NOT(MOD(12,SEQUENCE(1,12)))*SEQUENCE(1,12),(NOT(MOD(12,SEQUENCE(1,12)))*SEQUENCE(1,12))<>0)
Sign up to request clarification or add additional context in comments.

Comments

2

filter() will work, but you will have to enter your formula twice:

=FILTER(NOT(MOD(12,SEQUENCE(1,12)))*(SEQUENCE(1,12)),NOT(MOD(12,SEQUENCE(1,12)))*(SEQUENCE(1,12))<>0)

1 Comment

=LET(x,NOT(MOD(12,SEQUENCE(1,12)))*(SEQUENCE(1,12)),FILTER(x,x>0))
1

Use LET() function to avoid repetitive use of same formula. It will shorter your formula and improve performance. Try-

=LET(x,NOT(MOD(12,SEQUENCE(1,12)))*(SEQUENCE(1,12)),FILTER(x,x>0)) 

2 Comments

Hi Harun, since we're using LET to eliminate redundant calculations, a variable could also be defined for SEQUENCE. Having said that, NOT is already returning the Boolean values needed to filter the array, so the multiplication step can also be eliminated. I think something like this should do the trick: =LAMBDA(n,LET(j,SEQUENCE(,n),FILTER(j,NOT(MOD(n,j)))))(12) Kind regards. :)
Yes, this will improve performance of the formula. Thank you!
0

Another possible solution:

=TRANSPOSE(LET(n, 12, s, SEQUENCE(n), FILTER(s, MOD(n, s)=0)))

This formula finds all the factors of 12 and returns them in a single row. First, SEQUENCE(n) creates the numbers from 1 to n (in this case from 1 to 12). Then FILTER(s, MOD(n,s)=0) keeps only the numbers that divide 12 exactly, which are its factors. Finally, TRANSPOSE changes the list from a vertical column into a horizontal row. The LET function is used just to make the formula clearer by naming n and s inside the calculation.

Or even shorter (thanks to @VBasic2008; see comments below):

=LET(n, 12, s, SEQUENCE(,n), FILTER(s, MOD(n, s)=0))

4 Comments

FILTER works vertically and horizontally. SEQUENCE(1,n) or SEQUENCE(,n) will spill a row, so you don't need TRANSPOSE. Try it, don't take my word for it so it'll better sink in.
Thanks for your comment. I have just tried without TRANSPOSE, and the result is a column (and not a row).
You also have to change SEQUENCE(n) to either SEQUENCE(1,n) or SEQUENCE(,n).
You are right, VBasic2008: it works with SEQUENCE(1,n) or SEQUENCE(,n). Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.