Primes ’n’ Digits

11

2

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.

RamenChef

Posted 2017-12-23T18:00:18.620

Reputation: 1 163

Does 667 (=23*29) make for two 2s, one 3, and one 9 in step 3? – Jonathan Allan – 2017-12-23T18:10:45.453

@JonathanAllan Yes. – RamenChef – 2017-12-23T18:19:29.070

2@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. – Jonathan Allan – 2017-12-23T18:26:47.687

@JonathanAllan It would be nice if I could count. :-/ – wizzwizz4 – 2017-12-23T18:36:26.987

Answers

2

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

dylnan

Posted 2017-12-23T18:00:18.620

Reputation: 4 993

4

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

Jonathan Allan

Posted 2017-12-23T18:00:18.620

Reputation: 67 804

17 bytes – caird coinheringaahing – 2017-12-23T19:34:36.473

Or use dot product – H.PWiz – 2017-12-23T19:45:22.790

2

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

Uriel

Posted 2017-12-23T18:00:18.620

Reputation: 11 708

1

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!

DLosc

Posted 2017-12-23T18:00:18.620

Reputation: 21 213

1

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

Neil

Posted 2017-12-23T18:00:18.620

Reputation: 2 417

127 bytes – Mr. Xcoder – 2017-12-23T20:42:14.263

@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. – Neil – 2017-12-23T20:45:15.680

You can take a look through this for -~ and related stuff.

– Mr. Xcoder – 2017-12-23T20:48:43.757