Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Competitive programming #8619

Open
wants to merge 3 commits into
base: Competitive_Programming
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<n.\n",
"\n",
"\n",
"e.g :\n",
"\n",
"> Ξ¦(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<<sum<<endl;\n",
" delete[] phi;\n",
"}"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "H-uYnESAEdVo"
},
"source": [
"**APPLICATION:**\n",
"\n",
"1. **The RSA cryptosystem:**\n",
"\n",
"* Setting up an RSA system involves choosing large prime numbers p and q, computing n = pq and k = Ο†(n), and finding two numbers e and d such that ed ≑ 1 (mod k). The numbers n and e (the \"encryption key\") are released to the public, and d (the \"decryption key\") is kept private.\n",
"\n",
"* A message, represented by an integer m, where 0 < m < n, is encrypted by computing S = me (mod n).\n",
"\n",
"* It is decrypted by computing t = Sd (mod n). Euler's Theorem can be used to show that if 0 < t < n, then t = m.\n",
"\n",
"* The security of an RSA system would be compromised if the number n could be factored or if Ο†(n) could be computed without factoring n.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "p7poM8KdG4P6"
},
"source": [
"**REFERENCES:**\n",
"\n",
"\n",
"* wikipedia/euler's totient function\n",
"* cp-algorithms/euler's totient function\n",
"* geeksforgeeks/euler's totient function\n",
"\n"
]
}
]
}