Skip to content

Project Euler Problems in Hackerrank solved! #852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
19 changes: 19 additions & 0 deletions HackerRank/Project Euler/ProjectEuler1-Multiples-of-3-and-5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Author : Junth Basnet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a project_euler folder in root. Please put this into one of the folders inside.

"""
https://www.hackerrank.com/contests/projecteuler/challenges/euler001/problem
"""
def S(n):
return (n * (n + 1)) // 2

for _ in range(int(input())):

n = int(input())
#Sum of natural numbers below the given number
n -= 1

result = (S(n // 3) * 3) + (S(n // 5) * 5) - (S(n // 15) * 15)
print(result)




35 changes: 35 additions & 0 deletions sorts/QuickSort_MiddlePivot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Author : Junth Basnet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This can be merged.


"""
Implementation of Quick Sort Algorithm with middle element as pivot element
Time Complexity : O(nlogn) - O(n^2)
"""

def QuickSortFirst(array):
return QuickSort(array, 0, len(array) - 1)

def QuickSort(array, left, right):
if left >= right:
return array
pivot = array[(left + right) // 2]
index = Partition(array, left, right, pivot)
QuickSort(array, left, index - 1)
QuickSort(array, index, right)
return array

def Partition(array, left, right, pivot):
while left <= right:
while array[left] < pivot:
left += 1
while array[right] > pivot:
right -= 1
if left <= right:
array[left], array[right] = array[right], array[left]
left += 1
right -= 1
return left

array = [1, 6, 4, 10, 7, 30, 25]
print(array)
sorted_array = QuickSortFirst(array)
print(sorted_array)