11
\$\begingroup\$

This has no practical purpose but it could be fun to golf.

Challenge

Given a number n,

  1. Count the amount of each digit in n and add 1 to each count
  2. Take the prime factorization of n
  3. Count the amount of each digit in the prime factorization of n, without including duplicate primes
  4. Create a new list by multiplying together the respective elements of the lists from steps 1 and 3
  5. Return the sum of that list

For example, 121 has two 1s and a 2, so you would get the following list from step 1:

0 1 2 3 4 5 6 7 8 9
1 3 2 1 1 1 1 1 1 1

The prime factorization of 121 is 112, which gives the following list for step 3:

0 1 2 3 4 5 6 7 8 9
0 2 0 0 0 0 0 0 0 0

Note how we did not count the exponent. These multiply together to get:

0 1 2 3 4 5 6 7 8 9
0 6 0 0 0 0 0 0 0 0

And the sum of this list is 6.

Test cases

1 -> 0
2 -> 2
3 -> 2
4 -> 1
5 -> 2
10 -> 2
13 -> 4
121 -> 6

Notes

  • Standard loopholes are forbidden.
  • Input and output can be in any reasonable format.
  • You should leave ones (or zeros for step 3) in the list for digits that did not appear in the number.
  • This is , so the shortest solution in bytes wins.
\$\endgroup\$
  • \$\begingroup\$ Does 667 (=23*29) make for two 2s, one 3, and one 9 in step 3? \$\endgroup\$ – Jonathan Allan Dec 23 '17 at 18:10
  • \$\begingroup\$ @JonathanAllan Yes. \$\endgroup\$ – RamenChef Dec 23 '17 at 18:19
  • 2
    \$\begingroup\$ @wizzwizz4 232792560 -> [2,1,4,2,1,2,2,2,1,2] (step 1); 2*2*2*2*3*3*5*7*14*17*19 (step 2); so [0,5,1,2,0,1,0,2,0,1] (step 3); then [0,5,4,4,0,2,0,4,0,2] (Step 4); and hence should output 21. \$\endgroup\$ – Jonathan Allan Dec 23 '17 at 18:26
  • \$\begingroup\$ @JonathanAllan It would be nice if I could count. :-/ \$\endgroup\$ – wizzwizz4 Dec 23 '17 at 18:36
2
\$\begingroup\$

Jelly, 16 bytes

ṾċЀØD
ÆfQÇ×Ç‘$S

Try it online!

Developed independently from and not exactly the same as the other Jelly solution.

Explanation

I'm gong to use 242 as an example input.

ṾċЀØD     Helper link
Ṿ          Uneval. In this case, turns it's argument into a string. 
           242Ṿ → ['2','4','2']. [2,11] → ['2', ',', '1', '1']. The ',' won't end up doing anything.
    ØD     Digits: ['0','1',...,'9']
 ċЀ       Count the occurrence of €ach digit in the result of Ṿ

ÆfQÇ×Ç‘$S  Main link. Argument 242
Æf         Prime factors that multiply to 242 → [2,11,11]
  Q        Unique elements → [2,11]
   Ç       Apply helper link to this list → [0,2,1,0,0,0,0,0,0,0]
     Ç‘$   Apply helper link to 242 then add 1 to each element → [1,1,3,1,2,1,1,1,1,1]
    ×      Multiply the two lists element-wise → [0,2,3,0,0,0,0,0,0,0]
        S  Sum of the product → 5
\$\endgroup\$
4
\$\begingroup\$

Jelly,  18  17 bytes

-1 byte thanks to caird coinheringaahing & H.PWiz (avoid pairing the two vectors)

DF‘ċЀ⁵
ÆfQÇæ.Ç‘$

A monadic link taking a positive integer and returning a non-negative integer.

Try it online!

How?

DF‘ċЀ⁵ - Link 1, digitalCount: number(s)    e.g. [13,17]
D       - to decimal list (vectorises)            [[1,3],[1,7]]
 F      - flatten                                 [1,3,1,7]
  ‘     - increment (vectorises)                  [2,4,2,8]
      ⁵ - literal ten                             10
    Ѐ  - map across              (implicit range [1,2,3,4,5,6,7,8,9,10])
   ċ    - count                                   [0,2,0,1,0,0,0,1,0,0]

ÆfQÇæ.Ç‘$ - Main link: positive integer, n   e.g. 11999
        $ - last two links as a monad:
      Ç   -   call the last link (1) as a monad   [0,2,0,0,0,0,0,0,0,3]
       ‘  -   increment (vectorises)              [1,3,1,1,1,1,1,1,1,4]
Æf        - prime factorisation                   [13,13,71]
  Q       - deduplicate                           [13,17]
   Ç      - call the last link (1) as a monad     [0,2,0,1,0,0,0,1,0,0]
    æ.    - dot product                           8
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog), 43 41 bytes

⎕CY'dfns'
+/×/+/¨⎕D∘.=⍕¨(⎕D,r)(∪3pco r←⎕)

Try it online!

How?

r←⎕ - input into r

3pco - prime factors

- unique

⎕D,r - r prepended with 0-9

⍕¨ - format the factors and the prepended range

⎕D∘.= - cartesian comparison with every element of the string 0123456789

+/¨ - sum each row of the two tables formed

×/ - multiply the two vectors left

+/ - sum the last vector formed

\$\endgroup\$
1
\$\begingroup\$

Pip, 44 bytes

Y_N_.aM,tT++o>aTa%o{a/:olPBo}$+y*Y_N JUQlM,t

Takes input from command-line argument. Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 136 127 bytes

lambda a:sum(''.join(u(a)).count(`i`)*-~`a`.count(`i`)for i in range(10))
u=lambda a:[`j`for j in range(2,a)if a%j<1>len(u(j))]

Try it online!

Credits

\$\endgroup\$
  • \$\begingroup\$ 127 bytes \$\endgroup\$ – Mr. Xcoder Dec 23 '17 at 20:42
  • \$\begingroup\$ @Mr.Xcoder Updated, thanks for showing me the use of -~ I was always a bit confused on that. And I need to start remembering the <1 thing. Thanks for the help. \$\endgroup\$ – Neil Dec 23 '17 at 20:45
  • \$\begingroup\$ You can take a look through this for -~ and related stuff. \$\endgroup\$ – Mr. Xcoder Dec 23 '17 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.