Euler's Phi function is a function ϕ : N → N which when applied to a natural number n , gives the number of natural numbers less than or equal to n that are coprime to n .
Find n = 1 ∑ 1 0 0 0 ϕ ( n )
Suggestions :-
Solve this with a program in language you like, rather than using a calculator (Programming practice) ..... Make use of the properties of the Phi function.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Could you find the flaw in this computation:
primeList=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009 ]
def phi(num):
originalNum=num
for i in primeList:
if(i>(originalNum//2)+1):
break
elif(originalNum%i==0):
num=num*(1-(1/i))
return(int(num))
sum=0
for i in range(1,1001):
sum+=phi(i)
print(sum)
i am getting the answer as 303959. i checked the values of phi() for some random nos with your code and they came out to be exactly same!
Log in to reply
Could you explain why you have used the following condition in the if statement
if (i > (originalNum // 2) + 1):
Log in to reply
because the max value of a divisor of a number (apart from the number itself) can be (that number/2)
Log in to reply
@Vaibhav Ojha – If you divide any number by itself, then you get 1 which is an integer, therefore every number is its own divisor. You must also include the number itself along with other divisors.
If you compare your code with mine, you will notice that it gives different values of phi for all odd primes. Can you correct your code now?
Hint: One way to calculate ϕ ( 5 ) would be 5 × ( 1 − 5 1 ) = 4 . However your code calculates 5 × 1 = 5 .
Log in to reply
@Pranshu Gaba – Thanks! That sorts it out!
Log in to reply
@Vaibhav Ojha – You're welcome!
The new if condition must be
if (i > originalNum):
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
int c,ctr=0,sum=0;
for(c=1;c<=1000;c++)
{
for(int j=1;j<=c;j++)
{
int i,a=c,b=j,HCF,LCM;
if(a>b)
{
for(i=a%b;i!=0;i=a%b)
{
a=b;
b=i;
}
HCF=b;
}
else
{
for(i=b%a;i!=0;i=b%a)
{
b=a;
a=i;
}
HCF=a;
}
if(HCF==1)
ctr++;
}
}
System.out.println(ctr);
the ans will come 304192
in JAVA
I wrote following program in python for φ ( n ) , but it's returning wrong values...can someone help me to spot my mistake (since I'm beginner with python, I'm prone to mistakes)
Hello Rushikesh Jogdand , there's no need to define these many functions, you may read this note on the Phi function , it includes the way you wanted to achieve from the above efforts... :D
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Problem Loading...
Note Loading...
Set Loading...
This is my code in Python
It outputs 3 0 4 1 9 2