Skip to content

Commit 20a4fd5

Browse files
committed
Merge branch 'main' of https://github.com/Twiggecode/Integer-Sequences into main
2 parents 429ec92 + 4181aa4 commit 20a4fd5

35 files changed

+1351
-119
lines changed

Diff for: Alcuin Sequence/AlcuinSequence.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<stdio.h>
2+
#include<math.h>
3+
4+
/**
5+
* Alcuin Sequence
6+
* Formula: a(n) = round(n^2/12) – floor(n/4)*floor((n+2)/4)
7+
* Example: 0, 0, 0, 1, 0, 1, 1, 2, 1, 3, 2, 4, 3, 5, 4, 7 ...
8+
*/
9+
10+
// Algorithm for calculating Alcuin numbers
11+
int Alcuin(int n)
12+
{
13+
double _n = n, val;
14+
15+
val = round((_n * _n) / 12)
16+
- floor(_n / 4)
17+
* floor((_n + 2) / 4);
18+
19+
return val;
20+
}
21+
22+
// Main driver
23+
int main()
24+
{
25+
int n;
26+
printf("Enter the nth valu to find: ");
27+
scanf("%d",&n);
28+
printf("The term at the required index of the Alcuin Sequence is %d",Alcuin(n-1));
29+
return 0;
30+
}

Diff for: Alcuin Sequence/AlcuinSequence.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
/**
4+
* Alcuin Sequence
5+
* Formula: a(n) = round(n^2/12) – floor(n/4)*floor((n+2)/4)
6+
* Example: 0, 0, 0, 1, 0, 1, 1, 2, 1, 3, 2, 4, 3, 5, 4, 7 ...
7+
*/
8+
9+
// Algorithm for calculating Alcuin numbers
10+
int Alcuin(int n)
11+
{
12+
double _n = n, val;
13+
14+
val = round((_n * _n) / 12)
15+
- floor(_n / 4)
16+
* floor((_n + 2) / 4);
17+
18+
return val;
19+
}
20+
21+
// Main driver
22+
int main()
23+
{
24+
int n;
25+
cout<< "Enter the nth value to find: ";
26+
cin>> n;
27+
cout<< "The term at the required index of the Alcuin Sequence is ";
28+
cout<<Alcuin(n-1);
29+
return 0;
30+
}

Diff for: Alcuin Sequence/AlcuinSequence.py

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66

77
def alcuin(n):
8+
n = (
9+
n - 1
10+
) # Edit: The sequence begins with the coefficient of x^0, first term requires n = 0
811
return round((n * n) / 12) - floor(n / 4) * floor((n + 2) / 4)
912

1013

Diff for: Alcuin Sequence/Alcuin_Sequence.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void main(String[] args) {
1515
}
1616

1717
private static int Alcuin(int _n) {
18-
double n = _n;
18+
double n = _n-1; //Edit: The sequence begins with the coefficient of x^0, first term requires n = 0
1919
return (int)(Math.round((n * n) / 12) - Math.floor(n / 4) * Math.floor((n + 2) / 4));
2020
}
2121
}

Diff for: All Exponent Integer Series/pow(x,y).c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// C program for calculating x raised to the power of y
2+
// Time complexity O(log(n))
3+
4+
#include <stdio.h>
5+
6+
typedef unsigned long long U64;
7+
8+
U64 power(U64 x, U64 y)
9+
{
10+
U64 result = 1;
11+
while (y > 0) {
12+
if (y % 2 == 0) // y is even
13+
{
14+
x = x * x;
15+
y = y / 2;
16+
}
17+
else // y isn't even
18+
{
19+
result = result * x;
20+
y = y - 1;
21+
}
22+
}
23+
return result;
24+
}
25+
26+
// Driver Code
27+
int main()
28+
{
29+
U64 x,y;
30+
printf("Enter x in x^y: ");
31+
scanf("%llu", &x);
32+
printf("Enter y in x^y: ");
33+
scanf("%llu",&y);
34+
printf("%llu to power %llu is: %llu ",x,y,power(x,y));
35+
return 0;
36+
}
37+

Diff for: All Exponent Integer Series/pow(x,y).cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// Time complexity O(log(n))
44
#include <bits/stdc++.h>
55
using namespace std;
6-
7-
int power(int x, int y)
6+
7+
typedef unsigned long long U64;
8+
9+
U64 power(U64 x, U64 y)
810
{
9-
int result = 1;
11+
U64 result = 1;
1012
while (y > 0) {
1113
if (y % 2 == 0) // y is even
1214
{
@@ -25,11 +27,11 @@ int power(int x, int y)
2527
// Driver Code
2628
int main()
2729
{
28-
# just set the x,y to suit your needs
29-
int x,y;
30-
cin>>x>>y;
31-
32-
30+
U64 x,y;
31+
cout<<"Enter x in x^y: ";
32+
cin>>x;
33+
cout<<"Enter y in x^y: ";
34+
cin>>y;
3335
cout << (power(x, y));
3436
return 0;
3537
}

Diff for: All Exponent Integer Series/pow(x,y).py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#/usr/bin/python3
2+
3+
x=int(input("Enter x in x^y: "))
4+
y=int(input("Enter y in x^y: "))
5+
print(str(x)+"^"+str(y)+" is: "+str(pow(x,y)))

Diff for: Factorials/factorials.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
// Define the number of digits the output factorial can contain
5+
#define HUGE 10000
6+
7+
// High school multiplication - storing output as individual digits in an array
8+
// Multiplying each digit with individually and adjusting carry overs
9+
int mult(int x, int answer[], int output_digits)
10+
{
11+
int carry = 0;
12+
13+
for (int i=0; i<output_digits; i++)
14+
{
15+
int prod = answer[i] * x + carry;
16+
answer[i] = prod % 10;
17+
carry = prod/10;
18+
}
19+
20+
while (carry)
21+
{
22+
answer[output_digits] = carry%10;
23+
carry = carry/10;
24+
output_digits++;
25+
}
26+
return output_digits;
27+
}
28+
// Factorial function
29+
void factorial(int n)
30+
{
31+
int answer[HUGE];
32+
answer[0] = 1;
33+
int output_digits = 1;
34+
35+
for (int x=2; x<=n; x++)
36+
output_digits = mult(x, answer, output_digits);
37+
38+
cout << "Factorial of the given number is: \n";
39+
for (int i=output_digits-1; i>=0; i--)
40+
cout << answer[i];
41+
}
42+
// Main Driver
43+
int main()
44+
{
45+
int n;
46+
cout<<"Enter a number: ";
47+
cin>>n;
48+
factorial(n);
49+
return 0;
50+
}

Diff for: Factorials/factorials.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
1-
def factorial(a: int):
1+
"""
2+
In python default recursion limit is 1000.
3+
Naive recursive implementation will raise RecursionError for numbers >= 1000.
4+
By using memoization we can calculate factorials of those numbers
5+
step by step as shown in the main block.
6+
"""
7+
8+
9+
def factorial(a: int, results={}):
210
"""
311
handle edge case
412
a == 0 or 1 or -ve number
513
"""
14+
if a in results:
15+
return results[a]
16+
617
if a == 0 or a == 1:
718
return 1
819

920
if a < 0:
1021
return "Negative Integer is not allowed"
1122

1223
# recursive call
13-
return a * factorial(a - 1)
24+
result = a * factorial(a - 1)
25+
26+
# memoize the result
27+
results[a] = result
28+
29+
return result
1430

1531

1632
if __name__ == "__main__":
17-
n = int(input("Enter the positive integer value: "))
33+
n = 9
34+
answer = factorial(n)
35+
print(f"{n}! = {answer}")
36+
37+
n = 900
38+
answer = factorial(n)
39+
print(f"{n}! = {answer}")
40+
41+
n = 999
1842
answer = factorial(n)
1943
print(f"{n}! = {answer}")

Diff for: Fibonacci Sequence/fibonacci.py

+59-22
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,69 @@
1-
# This code will return the nth element of the Fibonacci sequence
1+
"""
2+
Calculate nth Fibonacci term
3+
"""
24

35

4-
def fibonacci(n):
5-
n += 1
6-
i = 1
7-
num1, num2 = 0, 1
6+
class NegativeNumber(Exception):
7+
pass
88

9-
if n == 1:
9+
10+
def fibonacci_iter(n):
11+
"""Iterative implementation of Fibonacci"""
12+
a, b = 0, 1
13+
14+
if n < 0:
15+
raise NegativeNumber("Fibonacci not defined for negative numbers!")
16+
17+
if n == 0:
1018
return 0
11-
elif n == 2:
12-
return 1
13-
else:
1419

15-
while i < n - 1:
20+
for _ in range(1, n):
21+
a, b = b, a + b
22+
23+
return b
24+
25+
26+
def fibonacci_naive_recur(n):
27+
"""Naive recursive computation of Fibonacci.
28+
Time complexity is O(1.6180 ^ n). It's big!"""
29+
30+
if n < 0:
31+
raise NegativeNumber("Fibonacci not defined for negative numbers!")
32+
33+
if n < 2:
34+
return n
35+
36+
return fibonacci_naive_recur(n - 1) + fibonacci_naive_recur(n - 2)
37+
38+
39+
def fibonacci(n, cache={}):
40+
"""Memoized recursive computation of Fibonacci.
41+
Time complexity is O(n). Better!"""
42+
if n < 0:
43+
raise NegativeNumber("Fibonacci not defined for negative numbers!")
44+
45+
if n < 2:
46+
return n
47+
48+
if n in cache:
49+
return cache[n]
50+
51+
cache[n] = fibonacci(n - 1) + fibonacci(n - 2)
1652

17-
sum = num1 + num2
18-
num1, num2 = num2, sum
19-
i += 1
20-
return sum
53+
return cache[n]
2154

2255

23-
while True:
24-
try:
25-
my_num = int(input("Enter a number:"))
26-
if my_num > -1:
27-
break
28-
except:
29-
print("Enter a positive integer: ")
56+
def tests(fib):
57+
assert fib(0) == 0
58+
assert fib(1) == 1
59+
assert fib(2) == 1
60+
assert fib(3) == 2
61+
assert fib(4) == 3
62+
assert fib(5) == 5
3063

3164

32-
print(f"The {my_num}th element of fibonacci series is:", fibonacci(my_num))
65+
if __name__ == "__main__":
66+
tests(fibonacci_iter)
67+
tests(fibonacci_naive_recur)
68+
tests(fibonacci)
69+
print(fibonacci_iter(10000))

Diff for: Gould Sequence/GouldSequence.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n,inp,count=0;
7+
cout<<"Enter the nth value: ";
8+
cin>>n;
9+
inp=n;
10+
while(inp)
11+
{
12+
count+=inp&1;
13+
inp>>=1;
14+
}
15+
cout<<n<<"th row in the pascal triangle has "<<(1<<count)<<" odd numbers!";
16+
return 0;
17+
}

0 commit comments

Comments
 (0)