From fda6ef7c73a75c683ac7f367f2377dd3e333c697 Mon Sep 17 00:00:00 2001 From: freq31 <70052807+freq31@users.noreply.github.com> Date: Sat, 27 Nov 2021 15:40:52 +0530 Subject: [PATCH 1/2] Add files via upload Labels: GWOC21, Competitive Programming, Documentation Fixes: https://github.com/girlscript/winter-of-contributing/issues/8618 Format: Documentation Language: C++ Topic: Number Theory Description: Explained proof of euler toitent function mathematically and wrote a code to calculate it for a particular number --- .../Euler_toitent_function.ipynb | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 Competitive_Programming/C++/Maths & Number Theory/Euler_toitent_function.ipynb 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..186d6a5b35 --- /dev/null +++ b/Competitive_Programming/C++/Maths & Number Theory/Euler_toitent_function.ipynb @@ -0,0 +1,141 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Euler_toitent_function.md", + "provenance": [] + }, + "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": "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 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", + "\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": "cc--D9uuj10W" + }, + "source": [ + "**FUNCTION TO CALCULATE EULER TOTIENT FUNCTION FOR A NATURAL NUMBER N:**" + ] + }, + { + "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": [] + } + ] +} \ No newline at end of file From bca60b4f40da2af8705962ac9e7ca699e5a21e34 Mon Sep 17 00:00:00 2001 From: freq31 <70052807+freq31@users.noreply.github.com> Date: Wed, 1 Dec 2021 19:01:48 +0530 Subject: [PATCH 2/2] Updated Document according to the suggested changes --- .../Euler_toitent_function.ipynb | 177 +++++++++++++++++- 1 file changed, 172 insertions(+), 5 deletions(-) diff --git a/Competitive_Programming/C++/Maths & Number Theory/Euler_toitent_function.ipynb b/Competitive_Programming/C++/Maths & Number Theory/Euler_toitent_function.ipynb index 186d6a5b35..d9d3a99b4b 100644 --- a/Competitive_Programming/C++/Maths & Number Theory/Euler_toitent_function.ipynb +++ b/Competitive_Programming/C++/Maths & Number Theory/Euler_toitent_function.ipynb @@ -4,7 +4,8 @@ "metadata": { "colab": { "name": "Euler_toitent_function.md", - "provenance": [] + "provenance": [], + "collapsed_sections": [] }, "kernelspec": { "name": "python3", @@ -24,13 +25,54 @@ "# **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" ] }, { @@ -64,7 +106,6 @@ "\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", - "\n", "> Then, Φ (n)=Φ ((p1^a)*(p2^b)......*(pk^k))\n", "\n", "> Using The Properties we get: Φ (n)= Φ ((p1^a))*Φ ((p2^b))*.....Φ ((pk^k)),\n", @@ -103,13 +144,31 @@ "\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:**" + "**FUNCTION TO CALCULATE EULER TOTIENT FUNCTION FOR A NATURAL NUMBER N:(IN C++)**" ] }, { @@ -136,6 +195,114 @@ ], "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<