Skip to content

Commit effb5e8

Browse files
committed
maths
1 parent cfb16e1 commit effb5e8

File tree

8 files changed

+159
-161
lines changed

8 files changed

+159
-161
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <algorithm>
5+
#include <utility>
6+
#include <iomanip>
7+
8+
using namespace std;
9+
10+
typedef unsigned long long int ulli;
11+
12+
vector<ulli> collatz(ulli number){
13+
14+
vector<ulli> seq;
15+
16+
while(number > 1){
17+
18+
if( number%2 == 1 ){
19+
number = (3*number) + 1;
20+
seq.push_back(number);
21+
}
22+
else{
23+
number = number/2;
24+
seq.push_back(number);
25+
}
26+
27+
}
28+
29+
return seq;
30+
}
31+
32+
33+
void printsequence(vector<ulli> &seq){
34+
35+
for(int i = 0;i < seq.size();i++){
36+
cout<<seq[i]<<" ";
37+
}
38+
cout<<endl;
39+
40+
}
41+
int main(){
42+
43+
ios_base::sync_with_stdio(false);
44+
45+
ulli number;
46+
cout<<"Enter the number for which collatz sequence is to be displayed : ";
47+
cin>>number;
48+
49+
vector<ulli> sequence;
50+
sequence = collatz(number);
51+
52+
printsequence(sequence);
53+
54+
return 0;
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def collatz(n):
2+
seq = [n]
3+
while n > 1:
4+
if n%2 == 1:
5+
n = n*3 + 1
6+
seq.append(n)
7+
else:
8+
n = n/2
9+
seq.append(n)
10+
return seq
11+
12+
if __name__ == "__main__" :
13+
number = input("Enter the number for which collatz sequence is to be displayed : ")
14+
print collatz(number)

Math/collatz_conjecture/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Collatz Conjecture #
2+
In the field of mathematics the collatz conjecture was posed by L. Collatz in 1937 which states that given a number n, one can always
3+
'get to one' by applying the following function recursively on the number.
4+
5+
## Function for Collatz Conjecture ##
6+
7+
1. **If number is even ** :<br />
8+
number = number / 2
9+
10+
2. **If number is odd ** :<br />
11+
number = (3 * number) + 1

Math/convuxhull/grahamscan.cpp

-161
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// A simple C program to calculate Euler's Totient Function
2+
#include <stdio.h>
3+
4+
// Function to return gcd of a and b
5+
int gcd(int a, int b)
6+
{
7+
if (a == 0)
8+
return b;
9+
return gcd(b%a, a);
10+
}
11+
12+
// A simple method to evaluate Euler Totient Function
13+
int phi(unsigned int n)
14+
{
15+
unsigned int result = 1;
16+
for (int i=2; i < n; i++)
17+
if (gcd(i, n) == 1)
18+
result++;
19+
return result;
20+
}
21+
22+
// Driver program to test above function
23+
int main()
24+
{
25+
int n;
26+
for (n=1; n<=10; n++)
27+
printf("phi(%d) = %d\n", n, phi(n));
28+
return 0;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def gcd(a, b):
2+
if a == 0:
3+
return b
4+
5+
return gcd(b % a, a)
6+
7+
def phi(n):
8+
res = 1
9+
10+
for i in range(2, n):
11+
if gcd(i, n) == 1:
12+
res += 1
13+
14+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from eulers_totient import phi
2+
3+
if __name__ == "__main__":
4+
for i in range(1, 11):
5+
print ("phi({}) == {}".format(i, phi(i)))
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Euler's Totient Function
2+
3+
Euler’s Totient function Φ(n) for an input n is count of numbers in {1, 2, 3, …, n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1.
4+
5+
```
6+
Φ(1) = 1
7+
gcd(1, 1) is 1
8+
9+
Φ(2) = 1
10+
gcd(1, 2) is 1, but gcd(2, 2) is 2.
11+
12+
Φ(3) = 2
13+
gcd(1, 3) is 1 and gcd(2, 3) is 1
14+
15+
Φ(4) = 2
16+
gcd(1, 4) is 1 and gcd(3, 4) is 1
17+
18+
Φ(5) = 4
19+
gcd(1, 5) is 1, gcd(2, 5) is 1,
20+
gcd(3, 5) is 1 and gcd(4, 5) is 1
21+
22+
Φ(6) = 2
23+
gcd(1, 6) is 1 and gcd(5, 6) is 1
24+
25+
```
26+
27+
### How to compute Φ(n) for an input n?
28+
29+
A simple solution is to iterate through all numbers from 1 to n-1 and count numbers with gcd with n as 1.
30+
31+

0 commit comments

Comments
 (0)