Compute Euler Phi Function of Large Integer | Matlab Solution

  • Context: MATLAB 
  • Thread starter Thread starter paton51
  • Start date Start date
  • Tags Tags
    Euler Function Phi
Click For Summary

Discussion Overview

The discussion revolves around computing the Euler phi function for large integers, specifically focusing on implementation in Matlab. Participants explore theoretical aspects, practical coding solutions, and alternative software recommendations.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant inquires about computing the Euler phi function for large integers and mentions knowledge of the function for prime numbers.
  • Another participant explains that the phi function is multiplicative and suggests factoring the integer to compute phi for each prime factor.
  • A suggestion is made to use Pari/GP instead of Matlab for number theory tasks, highlighting its efficiency with a specific command example.
  • A participant expresses intent to try implementing the function in Matlab and acknowledges the recommendation to explore Pari/GP.
  • Code snippets are shared for implementing the phi function in both Pari/GP and Matlab, with explanations of how the code operates.
  • A Matlab function is provided that computes the phi function for a vector of integers, demonstrating its utility in Matlab commands.

Areas of Agreement / Disagreement

Participants generally agree on the approach to compute the Euler phi function and the usefulness of alternative software like Pari/GP, but there is no consensus on the best method or tool for implementation.

Contextual Notes

Some limitations include the dependence on the efficiency of the factorization process and the potential differences in performance between Matlab and Pari/GP for large integers.

Who May Find This Useful

This discussion may be useful for individuals interested in number theory, programming in Matlab, or exploring efficient algorithms for mathematical functions.

paton51
Messages
8
Reaction score
0
How do i comput the euler phi function of a large interger?
i know that if p is prime then phi(p)=p-1 and I've found a formula for computing non primes but i don't know how to implement in something like Matlab.
Does anyone know how?
 
Physics news on Phys.org
What, Matlab doesn't have that already?

phi is a multiplicative function: phi(ab) = phi(a) * phi(b) if gcd(a, b) = 1. So to find phi(n), first factor n and then find phi(p^k) for each prime p dividing n exactly k times. Have you seen the formula for that? Can you prove it?

As for implementing the formula, just loop through the factor vector.
 
Last edited:
By the way, I assume this is for homework. If not, I strongly recommend Pari/GP rather than Matlab for number theory! The command in Pari/GP is eulerphi:

Code:
(11:09)eulerphi(10^50+35)
time = 375 ms.
%10 = 52844036689487355925892416914616365612135546307584
 
Ok thanks i'll have a go at it. I don't have PARI/GP but i mite try and download it, i generally use MATLAB cus it relativly easy. Its not a homework question, a lecture mentioned it and I am just trying to understand it!
Thanks for your comments!
 
No problem. I think that Pari is easier to use for these sorts of problems than Matlab, that's why I suggested it.

If you wanted to implement phi on your own in Pari (obviously it won't be quite as fast, but it'll help you understand):

Code:
myPhi(n)={
  local(f,t);
  f = factor(n);
  t = 1;
  for(i=1, #f[,1],
    t *= f[i, 1] - 1;
    t *= f[i, 1]^(f[i, 2] - 1);
  );
  t
}

The code loops through each prime component p^k and multiplies the running total t by p - 1 then by p^(k-1). For large n, most of the time is spent factoring (or proving primality) and so the code isn't actually that much worse in those cases.
 
I'll reply because this thread is a top Google hit:
Code:
function phi = eulerphi(n)
	phi = n;
	for i = 1:length(n)
		p = unique( factor(n(i)) );
		phi(i) = phi(i) * prod(1 - 1./p);
	end
end
The for-loop makes the function work with vectors, which is useful for MATLAB commands like "stem(1:100,eulerphi(1:100))".
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K