Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.21)
find_package(Python COMPONENTS Interpreter Development)
find_package(pybind11 CONFIG)

pybind11_add_module(cppCalc prime.cpp)
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ An intern has made a function to calculate all the unique prime factors for numb
Please include the following when you are writing your PR:
General things:
1. What is the purpose of this PR?
finsihed task 4

2. What changes did you make? Why?
computed upto sqrt(n), since that number squared would lead to the number

3. What bugs did you find while testing?
N/A. Slow tho

This PR Specific:
1. How does this make the code faster? How much headroom do you have?
Reduced the # of itnerations (runtime).

Tried Cpp with pybind, apparently that's slower?

Also I guess i could use sieve to first calculate all primes...
Binary file added cppCalc.so
Binary file not shown.
32 changes: 32 additions & 0 deletions prime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <pybind11/pybind11.h>
#include <iostream>
#include <vector>
#include <cmath>

namespace py = pybind11;

void get_prime_factors() {

for (int n = 2; n < 2500; n++){
int num = n;
std::vector<int> prime_factors;

while (num % 2 == 0) {
prime_factors.push_back(2);
num /= 2;
}
for (int i = 3; i <= sqrt(num); i += 2) {
while (num % i == 0) {
prime_factors.push_back(i);
num /= i;
}
}
if (num > 2) {
prime_factors.push_back(num);
}
}
}

PYBIND11_MODULE(cppCalc, m) {
m.def("disasterCode", &get_prime_factors, "A function to calculate prime");
}
54 changes: 36 additions & 18 deletions prime.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
import timeit
import math

# def disasterCode():
# for i in range (2,2500):
# uniquePrimes = []
# currentPrime = i
# for j in range (2,i):
# checkPrime = j
# flag = False
# for k in range (2,checkPrime-1):
# if (j%k==0):
# flag = True
# break
# if not flag and i%checkPrime==0 and checkPrime <= i:
# while (currentPrime%checkPrime==0):
# currentPrime/=checkPrime
# uniquePrimes.append(checkPrime)
# if len(uniquePrimes) == 0:
# uniquePrimes.append(i)
# print(uniquePrimes)

#this runs in 0.002 seconds
def disasterCode():
for i in range (2,2500):
uniquePrimes = []
currentPrime = i
for j in range (2,i):
checkPrime = j
flag = False
for k in range (2,checkPrime-1):
if (j%k==0):
flag = True
break
if not flag and i%checkPrime==0 and checkPrime <= i:
while (currentPrime%checkPrime==0):
currentPrime/=checkPrime
uniquePrimes.append(checkPrime)
if len(uniquePrimes) == 0:
uniquePrimes.append(i)
for i in range(2, 2500):
uniquePrimes = set()

n = i
while n % 2 == 0:
uniquePrimes.add(2)
n //= 2
for factor in range(3, int(math.sqrt(n)) + 1, 2):
while n % factor == 0:
uniquePrimes.add(factor)
n //= factor
if n > 2:
uniquePrimes.add(n)

#from cppCalc import disasterCode

# Benchmark the code
if __name__ == "__main__":
benchmark_code = "disasterCode()"
setup_code = "from __main__ import disasterCode"

# Measure the execution time of disasterCode function
times = []
for i in range(0,5):
times.append(timeit.timeit(benchmark_code, setup=setup_code, number=1))
Expand Down