-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotient.py
More file actions
81 lines (58 loc) · 1.39 KB
/
totient.py
File metadata and controls
81 lines (58 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 31 18:54:17 2019
@author: RAJDEEP PAL
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
#%%
# Function to return gcd of a and b
def gcd(a, b): # want a > b
if (a == b):
return a
elif ( b == 0 ):
return a
elif (a < b):
return gcd(b, a)
return gcd(b, b%a)
#%%
def gcd(a, b):
if (a == 0):
return b
return gcd(b % a, a)
#""" Euler Totient Function """
def phi(n):
result = 1
for i in range(2, n):
if (gcd(i, n) == 1):
result+=1
return result
#%%
# Driver Code
totient_vals = np.zeros(1000)
x = np.zeros(1000)
for i in range(1, 1001): # i = 1 to 1000
#print (i)
x[i-1] = i
totient_vals[i-1] = phi(i)
print ( phi(i) )
#%%
y = totient_vals
temp = x-1
font = {
# 'family' : 'Bitstream Vera Sans',
'family' : 'DejaVu Sans',
'weight' : 'bold',
'size' : 18
}
matplotlib.rc('font', **font)
plt.figure(figsize=(20, 20))
plt.plot(x, y, 'ro')
plt.title("Euler's Totient Function for the first 1000 positive integers")
#plt.legend(loc='upper right', shadow=True)
plt.ylabel('Totient values : phi(x)')
plt.xlabel('Integers : x')
plt.savefig("Totient Fuction")
# This code is contributed
# by Smitha