For a positive integer n with the prime factorization n = p1^e1 * p2^e2 * ... pk^ek where p1,...,pk are primes and e1,...,ek are positive integers, we can define two functions:
Ω(n) = e1+e2+...+ekthe number of prime divisors (counted with multiplicity) (A001222)ω(n) = kthe number of distinct prime divisors. (A001221)
With those two functions we define the excess e(n) = Ω(n) - ω(n) (A046660). This can be considered as a measure of how close a number is to being squarefree.
Challenge
For a given positive integer n return e(n).
Examples
For n = 12 = 2^2 * 3 we have Ω(12) = 2+1 and ω(12) = 2 and therefore e(12) = Ω(12) - ω(12) = 1. For any squarefree number n we obivously have e(n) = 0. The first few terms are
1 0
2 0
3 0
4 1
5 0
6 0
7 0
8 2
9 1
10 0
11 0
12 1
13 0
14 0
15 0
31 Answers 31
MATL, (削除) 7 (削除ここまで) 5 bytes
Yfd~s
Try it online! Or verify all test cases.
Explanation
Yf % Implicit input. Obtain prime factors, sorted and with repetitions
d % Consecutive differences
~ % Logical negate: zeros become 1, nonzeros become 0
s % Sum. Implicit display
-
\$\begingroup\$ I was not aware how
factorworks in MATL, really cool=) \$\endgroup\$flawr– flawr2016年08月28日 12:19:44 +00:00Commented Aug 28, 2016 at 12:19 -
\$\begingroup\$ @flawr Do you mean
YF(in the 7-byte version of the code) orYf(5-byte)? The latter is as in MATLAB \$\endgroup\$Luis Mendo– Luis Mendo2016年08月28日 12:20:45 +00:00Commented Aug 28, 2016 at 12:20 -
1\$\begingroup\$ The function for the exponents, the 5 byte now is even more clever=) \$\endgroup\$flawr– flawr2016年08月28日 12:22:01 +00:00Commented Aug 28, 2016 at 12:22
Brachylog, 11 bytes
$pPd:Pr:la-
Explanation
$pP P is the list of prime factors of the Input
Pd Remove all duplicates in P
:Pr Construct the list [P, P minus duplicates]
:la Apply "length" to the two elements of that list
- Output is the subtraction of the first element by the second one
Mathematica, 23 bytes
PrimeOmega@#-PrimeNu@#&
Very boring. FactorInteger already takes up 13 bytes, and I can't see much that can be done with the remaining 10.
Jelly, 5 bytes
ÆfI¬S
Port of Luis Mendo's answer in MATL.
ÆfI¬S
Æf Implicit input. Obtain prime factors, sorted and with repetitions
I Consecutive differences
¬ Logical negate: zeros become 1, nonzeros become 0
S Sum. Implicit display
-
\$\begingroup\$ For the previous approach,
ÆF’SṪwould have worked I think \$\endgroup\$Sp3000– Sp30002016年08月28日 12:31:59 +00:00Commented Aug 28, 2016 at 12:31 -
\$\begingroup\$ @Sp3000 You should post that \$\endgroup\$Leaky Nun– Leaky Nun2016年08月28日 12:32:24 +00:00Commented Aug 28, 2016 at 12:32
-
\$\begingroup\$ @LeakyNun I was trying to port it myself, but the definition of
¬confused me. I didn't know it vectorized \$\endgroup\$Luis Mendo– Luis Mendo2016年08月28日 12:42:13 +00:00Commented Aug 28, 2016 at 12:42 -
\$\begingroup\$ @LuisMendo Indeed the Jelly docs are messy. \$\endgroup\$Leaky Nun– Leaky Nun2016年08月28日 12:45:10 +00:00Commented Aug 28, 2016 at 12:45
-
1\$\begingroup\$
1#.1-~:@q:for 10 bytes. nice idea using~:btw. \$\endgroup\$Jonah– Jonah2019年02月04日 17:32:17 +00:00Commented Feb 4, 2019 at 17:32
Haskell, 65 bytes
(c%x)n|x>n=c|mod n x>0=c%(x+1)$n|y<-div n x=(c+0^mod y x)%x$y
0%2
-
\$\begingroup\$ if that is one function: who is the input variable? who is the output? thank u... \$\endgroup\$user58988– user589882016年08月30日 21:09:09 +00:00Commented Aug 30, 2016 at 21:09
-
\$\begingroup\$ (%) takes 3 input variables : an accumulator (c), an integer (x) and an integer (n) . It returns the excess of (n) when c is set to 0 and x to 2. So (0%2) is a partial function that takes n and returns its excess \$\endgroup\$Damien– Damien2016年08月31日 05:58:00 +00:00Commented Aug 31, 2016 at 5:58
05AB1E, 4 bytes
Ò\_O
Port of @LuisMendo's MATL answer.
Try it online or verify the first 15 test cases.
Explanation:
Ò # Get all prime factors with duplicates from the (implicit) input
# (automatically sorted from lowest to highest)
\ # Get all deltas
_ # Check if it's equal to 0 (0 becomes 1; everything else becomes 0)
O # Take the sum (and output implicitly)
-
\$\begingroup\$ 4 bytes... \$\endgroup\$Dominic van Essen– Dominic van Essen2020年10月29日 15:52:32 +00:00Commented Oct 29, 2020 at 15:52
Python 2, (削除) 57 (削除ここまで) 56 bytes
f=lambda n,k=2:n/k and[f(n,k+1),(n/k%k<1)+f(n/k)][n%k<1]
Thanks to @JonathanAllan for golfing off 1 byte!
Test it on Ideone.
-
\$\begingroup\$ Ah nice - you can save a byte by using
n/k%k<1\$\endgroup\$Jonathan Allan– Jonathan Allan2016年08月28日 22:40:58 +00:00Commented Aug 28, 2016 at 22:40 -
\$\begingroup\$ Right, n is already divisible by k at that point. Thanks! \$\endgroup\$Dennis– Dennis2016年08月28日 23:01:17 +00:00Commented Aug 28, 2016 at 23:01
Python 2, (削除) 100 (削除ここまで) (削除) 99 (削除ここまで) (削除) 98 (削除ここまで) 96 bytes
n=input()
i=2
f=[]
while i<n:
if n%i:i+=1
else:n/=i;f+=i,
if-~n:f+=n,
print len(f)-len(set(f))
Most of the code is taken up by a golfed version of this SO answer, which stores the prime factors of the input in f. Then we simply use set manipulation to calculate the excess factors.
Thanks to Leaky Nun for saving (削除) 1 (削除ここまで) 3 bytes!
Brachylog, 11 bytes
$p@b:la:-a+
Verify all testcases. (The wrapper is longer than the function...)
$p@b:la:-a+
$p prime factorization
@b group blocks of equal elements
:la length of each
:-a each minus 1
+ sum
S.I.L.O.S, 113 bytes
readIO
t=2
lbla
e=0
GOTO b
lblc
i/t
e+1
lblb
m=i
m%t
n=1
n-m
if n c
d=e
d/d
e-d
r+e
A=i
A-1
t+1
if A a
printInt r
A port of my answer in C.
-
\$\begingroup\$ So so close to beating C :( \$\endgroup\$Rohan Jhunjhunwala– Rohan Jhunjhunwala2016年08月28日 15:55:53 +00:00Commented Aug 28, 2016 at 15:55
Javascript (ES6), (削除) 53 (削除ここまで) (削除) 51 (削除ここまで) 46 bytes
e=(n,i=2)=>i<n?n%i?e(n,i+1):e(n/=i,i)+!(n%i):0
Saved 5 bytes thanks to Neil
Example:
e=(n,i=2)=>i<n?n%i?e(n,i+1):e(n/=i,i)+!(n%i):0
// computing e(n) for n in [1, 30]
for(var n = 1, list = []; n <= 30; n++) {
list.push(e(n));
}
console.log(list.join(','));
-
1\$\begingroup\$ You can save 5 bytes by calculating
rrecursively:f=(n,i=2)=>i<n?n%i?f(n,i+1):f(n/=i,i)+!(n%i):0. \$\endgroup\$Neil– Neil2016年08月28日 17:09:08 +00:00Commented Aug 28, 2016 at 17:09
Bash, 77 bytes
IFS=$'\n '
f=(`factor 1ドル`)
g=(`uniq<<<"${f[*]}"`)
echo $((${#f[*]}-${#g[*]}))
Complete program, with input in 1ドル and output to stdout.
We set IFS to begin with a newline, so that the expansion "${f[*]}" is newline-separated. We use arithmetic substitution to print the difference between the number of words in the factorisation with the result of filtering through uniq. The number itself is printed as a prefix by factor, but it is also present after filtering, so falls out in the subtraction.
Japt, 6 bytes
k äÎxÌ
k äÎxÌ :Implicit input of integer
k :Prime factors
ä :Consecutive pairs reduced by
Î : Signs of differences (0 or -1)
x :Sum of
Ì : Signs of differences with -1 (0 -> 1 and -1 -> 0)
Factor + math.primes.factors math.unicode, 40 bytes
[ group-factors unzip Σ swap length - ]
! 12
group-factors ! { { 2 2 } { 3 1 } }
unzip ! { 2 3 } { 2 1 }
Σ ! { 2 3 } 3
swap ! 3 { 2 3 }
length ! 3 2
- ! 1
Haskell, 87 bytes
import Data.Numbers.Primes
import Data.List
x=(\x->length x-length(nub x)).primeFactors
Nekomata, 3 bytes
ƒ←∑
ƒ Factor; returns a list of unique prime factors and a list of their exponents
← Decrement the exponents
∑ Sum
Python, (with sympy) 66 bytes
import sympy;lambda n:sum(x-1for x in sympy.factorint(n).values())
sympy.factorint returns a dictionary with factors as keys and their multiplicities as values, so the sum of the values is Ω(n) and the number of values is ω(n), so the sum of the decremented values is what we want.
CJam, 11 bytes
ri_mf,\mF,-
Explanation
ri_ Get an integer from input and duplicate it
mf, Get the number of prime factors (with repetition)
\ Swap top 2 elements on the stack
mF, Get the number of prime factors (with exponents)
- Subtract
C, 158
#define G(a,b) if(a)goto b
#define R return
f(n,i,j,o,w){if(!n)R 0;o=w=i=j=0;a:i+=2;b:if(n%i==0){++j;G(n/=i,b);}o+=!!j;w+=j;i+=(i==2);j=0;G(i*i<n,a);R w-o;}
In the start there is the goto instruction... even if this is more long than yours it is more readable and right [if i not consider n too much big...] one Language that has 10000 library functions is wrost than a Language that with few, 20 or 30 library functions can do all better [because we can not remember all these functions]
#define F for
#define P printf
main(i,r)
{F(i=0; i<100; ++i)
r=f(i,0,0,0,0),P("[%u|%u]",i,r);
R 0;
}
/*
158
[0|0][1|0][2|0][3|0][4|1][5|0][6|0][7|0][8|2]
[9|0][10|0][11|0][12|1][13|0][14|0][15|0][16|3]
[17|0][18|0][19|0][20|1][21|0][22|0][23|0][24|2][25|1][26|0][27|0] [28|1]
[29|0][30|0][31|0][32|4][33|0][34|0][35|0][36|1]
[37|0][38|0][39|0][40|2][41|0]
*/
GNU sed + coreutils, 55 bytes
(including +1 for -r flag)
s/^/factor /e
s/ ([^ ]+)(( 1円)*)/2円/g
s/[^ ]//g
y/ /1/
Input in decimal, on stdin; output in unary, on stdout.
Explanation
#!/bin/sed -rf
# factor the number
s/^/factor /e
# remove first of each number repeated 0 or more times
s/ ([^ ]+)(( 1円)*)/2円/g
# count just the spaces
s/[^ ]//g
y/ /1/
APL(NARS) 35 chars, 70 bytes
{⍵=1:0⋄k-⍨+/+/ ̈{w=⍵⊃v} ̈⍳k←≢v←∪w←π⍵}
the function π find the factorization in prime of its argument; there is few to say it seems clear, but for me does more operations (from factorization) than the minimum...the range of count characters is out golfing languages because it seems too much count, but less than not golfing languages... test:
f←{⍵=1:0⋄k-⍨+/+/ ̈{w=⍵⊃v} ̈⍳k←≢v←∪w←π⍵}
f ̈1..15
0 0 0 1 0 0 0 2 1 0 0 1 0 0 0
Explore related questions
See similar questions with these tags.
^is power \$\endgroup\$