Skip to content

Commit 91e73de

Browse files
committed
Added python code for problems in math section
1 parent f811043 commit 91e73de

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Print all factors of a given number
3+
4+
"""
5+
6+
#Enter your number
7+
num = input("Enter a number: ");
8+
#initialize your count as 0
9+
count = 0;
10+
number = int(num);
11+
#run a loop to find factors
12+
#Check the factors (starting with 2, until the number is reached)
13+
for i in range(2, number-1):
14+
#If the divided number has 0 as the division reminder, it'll be a factor
15+
if number%i == 0:
16+
#Print this number everytime
17+
print(i);
18+
i += 1;
19+
count += 1;
20+
#If number is not divisible by any number other than 1 and number itself,then it is prime
21+
if count==0:
22+
print(number,"is a prime number.");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
Print all permutations of a given list
3+
4+
"""
5+
6+
#We can do it by built-in permutation function in itertools library
7+
#import the itertools library
8+
import itertools
9+
#call the permutation function of itertools library and pass list numbers as a argument
10+
#this will print the list of permutations
11+
print(list(itertools.permutations([1, 2, 3])))

Math/NextPow2/Python/nextpow2.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Print next power of 2 for a given number
3+
4+
"""
5+
#import the math library
6+
import math
7+
#function which calculates the nextpow of 2
8+
def nextpow(x):
9+
#this would give us the power P , such that 2 ^ P = X.
10+
#ceil function to get the next number
11+
nextnum = float(math.ceil(math.log(x,2)))
12+
#2 ^ next number will be the required result.
13+
result = float(pow(2.0,nextnum))
14+
return int(result)
15+
16+
#Enter your number
17+
num = int(input("Enter a number: "))
18+
#call a function
19+
ans = nextpow(num)
20+
#print answer
21+
print(ans)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Tower Of Hanoi
3+
4+
"""
5+
#MoveTower function which uses recursion
6+
def moveTower(height,fromPole, toPole, withPole):
7+
if height >= 1:
8+
moveTower(height-1,fromPole,withPole,toPole)
9+
moveDisk(fromPole,toPole)
10+
moveTower(height-1,withPole,toPole,fromPole)
11+
12+
#Printing the movements of disks
13+
def moveDisk(fp,tp):
14+
print("moving disk from",fp,"to",tp)
15+
16+
#Enter number of disks
17+
n = int(input("Enter number of disks "))
18+
#call the function
19+
moveTower(n,"A","B","C")

Math/floor_sqrt/C++/sqrt.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// A C++ program to find floor(sqrt(x) using binary search
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
// Returns floor of square root of x
6+
int floorSqrt(int x)
7+
{
8+
// Base cases
9+
if (x == 0 || x == 1)
10+
return x;
11+
12+
// Staring from 1, try all numbers until
13+
// i*i is greater than or equal to x.
14+
int i = 1, result = 1;
15+
while (result <= x)
16+
{
17+
i++;
18+
result = i * i;
19+
}
20+
return i - 1;
21+
}
22+
23+
int main()
24+
{
25+
int x;
26+
cout<<"Enter a number"<<endl;
27+
cin>>x;
28+
cout << "The floor square root of an integer is "<< floorSqrt(x) << endl;
29+
return 0;
30+
}

0 commit comments

Comments
 (0)