Factors of an integer: Difference between revisions
Content added Content deleted
m (No, it's a real language of programmable calculators.) |
(J: use a more structurally illustrative examples and some other changes to hopefully make the concepts clearer) |
||
Line 997: | Line 997: | ||
Alternatively, q: can produce provide a table of the exponents of the unique relevant prime factors |
Alternatively, q: can produce provide a table of the exponents of the unique relevant prime factors |
||
<lang J>__ q: |
<lang J> __ q: 420 |
||
2 3 5 7 |
|||
2 1 1 1</lang> |
|||
With this, we can form lists of each of the potential relevant powers of each of these prime factors |
With this, we can form lists of each of the potential relevant powers of each of these prime factors |
||
<lang J>((^ i.@>:)&.>/) __ q: |
<lang J> ((^ i.@>:)&.>/) __ q: 420 |
||
┌─────┬───┬───┬───┐ |
|||
┌───────┬───┐ |
|||
│1 2 4│1 3│1 5│1 7│ |
|||
└─────┴───┴───┴───┘</lang> |
|||
└───────┴───┘ |
|||
From here, it's a simple matter (<code>*/&>@{</code>) to compute all possible factors of the original number |
From here, it's a simple matter (<code>*/&>@{</code>) to compute all possible factors of the original number |
||
<lang J> |
<lang J>factrs=: */&>@{@((^ i.@>:)&.>/)@q:~&__ |
||
factrs 40 |
|||
⚫ | |||
1 5 |
1 5 |
||
2 10 |
2 10 |
||
Line 1,016: | Line 1,015: | ||
8 40</lang> |
8 40</lang> |
||
However, a data structure which is organized around the prime decomposition of the argument can be hard to read. So, for reader convenience, we should probably arrange them in a monotonically increasing list: |
|||
⚫ | |||
⚫ | |||
<lang J> factors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
1 2 3 4 5 6 7 10 12 14 15 20 21 28 30 35 42 60 70 84 105 140 210 420</lang> |
|||
)</lang> |
|||
A less efficient, but concise variation on this theme: |
A less efficient, but concise variation on this theme: |
||
Line 1,029: | Line 1,028: | ||
This computes 2^n intermediate values where n is the number of prime factors of the original number. |
This computes 2^n intermediate values where n is the number of prime factors of the original number. |
||
⚫ | |||
<lang J>factors 40 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
) |
|||
factorsOfNumber 40 |
|||
1 2 4 5 8 10 20 40</lang> |
1 2 4 5 8 10 20 40</lang> |
||