Euler continued to investigate properties of numbers, specifically the distribution of prime numbers. One important function he defined is called the phi function. It measures the breakability of a number. So, given a number, say N, it outputs how many integers are less than or equal to N that do not share any common factor. If this group is associated with some arithmetic function then the Cayley graph becomes an Arithmetic graph. In this paper, we study independent domination number of Euler totient Cayley graphs and Arithmetic graphs. Key words: Dominating set, Independent dominating set, Euler totient Cayley graph, Arithmetic graph.
Active1 year, 1 month ago
I am trying to find an efficient way to compute Euler's totient function.
What is wrong with this code? It doesn't seem to be working.
Ry-♦177k42 gold badges364 silver badges381 bronze badges
Brock WestBrock West
7 Answers
Here's a much faster, working way, based on this description on Wikipedia:
Thus if n is a positive integer, then φ(n) is the number of integers k in the range 1 ≤ k ≤ n for which gcd(n, k) = 1.
I'm not saying this is the fastest or cleanest, but it works.
rumpel5,4561 gold badge23 silver badges33 bronze badges
orlporlp![Euler totient function in python Euler totient function in python](/uploads/1/2/6/2/126265771/511117099.png)
71.4k27 gold badges166 silver badges251 bronze badges
You have three different problems...
y
needs to be equal ton
as initial value, not1
- As some have mentioned in the comments, don't use integer division
n % i 0 is True
isn't doing what you think because of Python chaining the comparisons! Even ifn % i
equals0
then0 0
isTrue
BUT0 is True
isFalse
! Use parens or just get rid of comparing toTrue
since that isn't necessary anyway.
Fixing those problems,
JaredJared16.7k7 gold badges41 silver badges58 bronze badges
I'm working on a cryptographic library in python and this is what i'm using.
gcd()
is Euclid's method for calculating greatest common divisor, and phi()
is the totient function.SeriniceSerinice
It looks like you're trying to use Euler's product formula, but you're not calculating the number of primes which divide a. You're calculating the number of elements relatively prime to a.
In addition, since 1 and i are both integers, so is the division, in this case you always get 0.
JoelJoel5,1791 gold badge16 silver badges18 bronze badges
With regards to efficiency, I haven't noticed anyone mention that gcd(k,n)=gcd(n-k,n). Using this fact can save roughly half the work needed for the methods involving the use of the gcd. Just start the count with 2 (because 1/n and (n-1)/k will always be irreducible) and add 2 each time the gcd is one.
Bill BellBill Bell16.5k4 gold badges30 silver badges44 bronze badges
Calculating gcd for every pair in range is not efficient and does not scales. You don't need to iterate throught all the range, if n is not a prime you can check for prime factors up to its square root, refer to https://stackoverflow.com/a/5811176/3393095. We must then update phi for every prime by phi = phi*(1 - 1/prime).
Rodrigo LópezRodrigo López
Actually to calculate phi(any number say n)
We use the Formula
where p are the prime factors of n.
We use the Formula
where p are the prime factors of n.
So, you have few mistakes in your code:
1.
2. For
1.
y
should be equal to n
2. For
1/i
actually 1
and i
both are integers so their evaluation will also be an integer,thus it will lead to wrong results.Here is the code with required corrections.
alphaguyalphaguy
Not the answer you're looking for? Browse other questions tagged python or ask your own question.
Active6 years, 9 months ago
$begingroup$If you look the at the wikipedia page for the Euler totient function there is a graph of the Euler phi function up to 1000:
![Function Function](/uploads/1/2/6/2/126265771/954427472.png)
Out of the plot you can see various trend lines that appear to be linear.
Q1) Are these lines actually lines? (ie do all the points that appear to be on one line actually all solutions to some linear eqn?)
Q2) Given that they're lines and not just something that looks a bit like lines can they be described? and what points are on them?
Q3) Are the points that don't appear to be on lines actually just the first points on lines that would only be 'visible' if we zoomed out and evaluated the Euler phi up to (say) 10000 or whatever number is large enough.
Zev Chonoles112k16 gold badges244 silver badges443 bronze badges
Kate SignosKate Signos
$endgroup$3 Answers
$begingroup$In the following $mathbb{P}$ denotes the set of primes and $pinmathbb{P}$ a prime.
We know $phi(p)=p-1$, so that ${(p,p-1):pinmathbb{P}}$ is contained in the graph of $phi$. This set lies on the line $y=x-1$, and corresponds to the steepest line in the graph.
Fix $qinmathbb{N}$. Then $phi(qcdot p)=phi(q)(p-1)$ if $pnotmid q$. The set of points ${(qcdot p,phi(q)(p-1)):pnotmid q}$ is contained in the graph of $phi$ and lies on the line$$y=phi(q)Bigl(frac{x}{q}-1Bigr).$$The other lines you see correspond to $q=2$, $3$ and $4$.
Julián AguirreJulián Aguirre70.2k2 gold badges42 silver badges98 bronze badges
$endgroup$$begingroup$Some of the 'dotted lines' that you're looking at are actually points from lines close to each other, but yes there are some lines.
Most obviously, the upper bound is the line $y=x-1$, which is hit by all the primes.
Going along that same line of thought, think of $phi(3p)$ for primes $p$ other than $3$. You can compute that $phi(3p)=2phi(p)$. This says that if you went and plotted all the values $3p$ for primes other than $3$, you would get the dotted line of points $(3p, 2phi(p))$ lying on the line $y=2(frac{x}{3}-1)$.
I'm not sure this can be developed into a complete answer about lines, but it certainly seems to paint a picture for the $n$ which are divisible by a prime $q$ but not by $q^2$. I would suspect that numbers which do not have prime factors with multiplicity 1 are the 'stray' points.
rschwiebrschwieb114k12 gold badges113 silver badges268 bronze badges
$endgroup$$begingroup$Since $frac{phi(n)}n=prod_{pmid n}frac{p-1}p$, where the product is over all distinct prime factors, the lines are almost through the origin and mostly exhibit the distribution of distinct small prime factors in $n$, where the eye does not distinguish the differences between the factor $frac{p-1}p$ for the largest prime factor, which is usually different between the values of $n$ on the same 'line'.
So the topmost line is for the prime numbers themselves, where $phi(n)=n-1$, is a straight line almost through the origin; the 'halfway' line is for numbers $n=2^kp^l$ (for some relatively large prime $p$); it is almost equal to the line through the origin with slope $frac12$. In fact it is an almost straight line $phi(n)=frac n2-frac n{2p}$ that holds for such numbers, with downards dents when the final factor $p$ is not so very large. Other 'lines' can be explained similarly, but are less and less marked. And none of these lines are straight; if you look carefully there are points that tend to 'droop'.
Added: in fact one can explain these non-straight lines also as a union of several (in principle infinitely many) very close straight lines and lots of other points nearby. For instance the 'line' for values $n=2^kp^l$ can be split into one for $n=2p$, where $phi(n)=(p-1)=frac n2-1$, one for $n=4p$, where $phi(n)=2(p-1)=frac n2-2$, ..., one for $n=2^kp$ where $phi(n)=2^{k-1}(p-1)=frac n2-2^{k-1}$ (which is still relatively close to $y=frac n2$ when $p$ is very large), and other with higher powers of $p$, for instance $n=2p^2$ given $phi(n)=p(p-1)=frac n2-sqrt{frac n2}$ which is not a straight line but still relatively close to the line $y=frac n2$. Indeed even other points would contribute visually to a clustering around (or more precisely just below) the line $y=frac n2$: when $m=2pq$ is twice a product of two large primes $p<q$, then $phi(m)=(p-1)(q-1)$ is less than $2q=frac mp$ away from that line. The same occurs by the way just below the line $y=x-1$ containing $(p,phi(p))$ for any prime $p$: any number that only has few and very large prime factors will produce a point just below that line.
In the given graph I can only distinguish four lines clearly: the top one with slope $1$ for the prime numbers, the line with slope $frac12$ for the numbers with only $2$ as small prime factor, the line with slope $frac23$ for the numbers with only $3$ as small prime factor, and the line with slope $frac13$ for the numbers with only $2$ and $3$ as small prime factors. Knowing the phenomenon one can discern lines with slopes $frac45$ and $frac67$, but already these don't stand out very clearly.
Marc van LeeuwenMarc van Leeuwen92.1k6 gold badges115 silver badges250 bronze badges
$endgroup$