diff --git a/Competitive_Programming/C++/Maths & Number Theory/Euler_toitent_function.ipynb b/Competitive_Programming/C++/Maths & Number Theory/Euler_toitent_function.ipynb new file mode 100644 index 0000000000..d9d3a99b4b --- /dev/null +++ b/Competitive_Programming/C++/Maths & Number Theory/Euler_toitent_function.ipynb @@ -0,0 +1,308 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Euler_toitent_function.md", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "dP9LH9ODdmir" + }, + "source": [ + "# **EULER TOITENT FUNCTION:**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4WWVz2rf8MLD" + }, + "source": [ + "**PROBLEM STATEMENT:**\n", + "\n", + "* The Objective is to explain the euler totient function algorithm,analyse its complexity and working ,and state its uses in computation\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UCesKC4Jd6yA" + }, + "source": [ + "Euler’s Totient function Φ (n) for an input n is the count of numbers belonging to set {1,2,3,....n} such that the number is coprime with n, i.e gcd(m,n)=1 such that 1<=m Φ(1) = 1 \n", + "gcd(1, 1) is 1\n", + "\n", + "> Φ(2) = 1\n", + "gcd(1, 2) is 1, but gcd(2, 2) is 2.\n", + "\n", + "\n", + ">Φ(3) = 2\n", + "gcd(1, 3) is 1 and gcd(2, 3) is 1\n", + "\n", + "> Φ(4) = 2\n", + "gcd(1, 4) is 1 and gcd(3, 4) is 1\n", + "\n", + "\n", + "> Φ(5) = 4\n", + "gcd(1, 5) is 1, gcd(2, 5) is 1, \n", + "gcd(3, 5) is 1 and gcd(4, 5) is 1\n", + "\n", + "\n", + "> Φ(6) = 2\n", + "gcd(1, 6) is 1 and gcd(5, 6) is 1,\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "45B_hNXcefjl" + }, + "source": [ + "**PROPERTIES:**\n", + "\n", + "\n", + "1. Φ (n)= n*(1-1/p1)*(1-1/p2)....*(1-1/pk), where p1,p2 .....,pk are prime factors of n.\n", + "\n", + "2. Φ (p)=p-1, where p=prime number\n", + "\n", + "3. Φ (ab)=Φ (a)*Φ (b)\n", + "\n", + "4. Φ (p^k)=(p^k)-(p^(k-1)) ,where p=prime number\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XquPBAxYgZ3z" + }, + "source": [ + "**PROOF:**\n", + "\n", + "> Let n=(p1^a)*(p2^b)......*(pk^k) ,where p1,p2 .....,pk are prime factors of n and a,b,....k are powers of prime factors of n.\n", + "\n", + "> Then, Φ (n)=Φ ((p1^a)*(p2^b)......*(pk^k))\n", + "\n", + "> Using The Properties we get: Φ (n)= Φ ((p1^a))*Φ ((p2^b))*.....Φ ((pk^k)),\n", + "\n", + "\n", + "> Since,p1,p2,...pk are prime numbers Therefore,Using the Properties Φ (pi^x)=(pi^x)-(pi^(x-1))\n", + "\n", + "\n", + "> => Φ (pi^x) = (pi^x)*(1-1/pi)\n", + "\n", + "> => Φ (n)= ((p1^a)*(1-1/p1))*((p2^b)*(1-1/p2))*........((pk^k)*(1-1/pk))\n", + "\n", + "\n", + "> => Φ (n) = ((p1^a)*(p2^b)......*(pk^k))*(1-1/p1)*(1-1/p2)*.....(1-1/pk)\n", + "\n", + "\n", + "> Therefore, Φ (n) = n*(1-1/p1)*(1-1/p2)*.....(1-1/pk)\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "j9zS8ZH8-cNt" + }, + "source": [ + "**ALGORITHM:**\n", + "\n", + "\n", + "1. CREATE AN ARRAY PHI OF SIZE N+1 AND INITIALIZE iTh ELEMENT WITH VALUE i ,i={1,2,.....N}\n", + "2. RUN A LOOP FROM 2 TO N, AND IF ELEMENT IN ARRAY PHI IS EQUAL TO ITS INDEX(WHICH MEANS ELEMENT IS PRIME),CHANGE ITS VALUE TO INDEX-1\n", + "3. FOR ALL MULTIPLES OF THE CURRENT INDEX LESS THAN EQUAL TO N,MULTIPLY THE ELEMENTS WITH THE VALUE= (1-1/MULTIPLE).\n", + "4. RETURN VALUE AT PHI[N]\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cc--D9uuj10W" + }, + "source": [ + "**FUNCTION TO CALCULATE EULER TOTIENT FUNCTION FOR A NATURAL NUMBER N:(IN C++)**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ku3SKXSEkFZN" + }, + "source": [ + "int euler_totient_function(int n){\n", + " int phi[n+1];\n", + " for(int i=1;i<=n;i++){\n", + " phi[i]=i;\n", + " }\n", + " for(int i=2;i<=n;i++){\n", + " if(phi[i]==i){\n", + " phi[i]=i-1;\n", + " for(int j=2*i;j<=n;j+=i){\n", + " phi[j]=(phi[j]*(i-1))/i;\n", + " }\n", + " }\n", + " }\n", + " return phi[n];\n", + "}" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3cTCqyagBPoX" + }, + "source": [ + "**COMPLEXITY ANALYSIS:**\n", + "\n", + "\n", + "1. SPACE COMPLEXITY:\n", + "SINCE,WE ARE USING ONLY AN EXTRA ARRAY OF SIZE N+1,SO OUR SPACE COMPLEXITY IS **O(n)**\n", + "\n", + "2. TIME COMPLEXITY:\n", + "Since,We start our iteration from i=2 and go till the last multiple of 2 less than equal to n .\n", + "Therefore,our time complexity would be N/2 + N/3 +N/5 +N/7+.....N/p;where p is the last prime less than equal to n\n", + "This Sum comes out to be Nlog(log(N))\n", + "Thus,Our Time Complexity is **O(Nlog(log(N)))**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8nnAF_Q7HaFi" + }, + "source": [ + "**SOLVING A PROBLEM BASED ON EULER TOTIENT FUNCTION:**\n", + "\n", + "PROBLEM STATEMENT:\n", + "Given n, calculate and print the sum :\n", + "LCM(1,n) + LCM(2,n) + .. + LCM(n,n)\n", + "where LCM(i,n) denotes the Least Common Multiple of the integers i and n.\n", + "\n", + "ALGORITHM:\n", + "USING LCM,GCD AND EULER'S TOTIENT FUNCTION PROPERTIES WE ARRIVE AT THE LCM SUM AS, **LCM SUM = n/2*(SUM(Φ (d)*d)+1) ,Where d=all divisors of n except 1**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "hZGlLO17IMKz" + }, + "source": [ + "void euler(long long n,long long *phi){\n", + " for(int i=1;i<=n;i++){\n", + " phi[i]=i;\n", + " }\n", + " for(int i=2;i<=n;i++){\n", + " if(phi[i]==i){\n", + " phi[i]=i-1;\n", + " for(int j=2*i;j<=n;j+=i){\n", + " phi[j]=(phi[j]*(i-1))/i;\n", + " }\n", + " }\n", + " }\n", + "}\n", + "void func(long long n)\n", + "{\n", + " long long *phi=new long long[n+1];\n", + " euler(n,phi);\n", + " long long sum=0;\n", + " for(int i=1;i<=n;i++){\n", + " if(n%i==0){\n", + " sum+=(phi[i]*i);\n", + " }\n", + " }\n", + " sum=((sum+1)*n)/2;\n", + " cout<