You are currently viewing Best way to select arrays by frequency of appearance of nums of interest

Best way to select arrays by frequency of appearance of nums of interest

Hi, I have this very rudimentary code which yields the prime factors of some numbers (say from 2 to 20). The following is ran on the variable ssl, the range (2..20) converted to strings (the getPrimeFactors requires BigInteger() which I learnt in Kotlin must be fed strings.

ssl.map{getPrimeFactors(BigInteger(it))}.map{ it.count()} 

The result looks like

[[2], [3], [2, 2], [5], [2, 3], [7], [2, 2, 2], [3, 3], [2, 5], [11], [2, 2, 3], [13], [2, 7], [3, 5], [2, 2, 2, 2], [17], [2, 3, 3], [19], [2, 2, 5]] 

I now want to select the entries where the frequencies are the largest prime frequencies. In this example, for 2, it would be 2^3 as that is the highest power of 2 that is also a prime in this list. I want to keep every such “largest” prime power for all the numbers here. I later on need to multiply these together. So, keep 19 (coz that is the largest prime power of 19 there), keep 17 for the same reason… etc. Hopefully this is clear.

I tried using groupingBy{it}.eachCount() instead of the count() above, and got

[{2=1}, {3=1}, {2=2}, {5=1}, {2=1, 3=1}, {7=1}, {2=3}, {3=2}, {2=1, 5=1}, {11=1}, {2=2, 3=1}, {13=1}, {2=1, 7=1}, {3=1, 5=1}, {2=4}, {17=1}, {2=1, 3=2}, {19=1}, {2=2, 5=1}] 

submitted by /u/newcomer_l
[link] [comments]