Skip to content

Instantly share code, notes, and snippets.

View aadimator's full-sized avatar

Aadam aadimator

View GitHub Profile
@aadimator
aadimator / waitForUser.cpp
Last active August 26, 2015 09:58
waitForUser();
void waitForUser () {
cout << "Press Enter to continue...." << endl;
if (cin.get() != '\n') cin.ignore(100, '\n');
}
@aadimator
aadimator / 0_reuse_code.js
Last active August 29, 2015 14:26
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
void bubble_sort(int Array[], int size)
{
bool sorted = false; // assume that the array is not sorted
while (!sorted)
{
bool swaped = false;
for (int i = 0; i < size; i++)
{
if (Array[i] > Array[i+1])
{
@aadimator
aadimator / fib.cpp
Last active July 19, 2016 05:44
Small Fibonacci Number
#include <iostream>
int calc_fib(int n) {
if (n <= 1)
return n;
return calc_fib(n - 1) + calc_fib(n - 2);
}
@aadimator
aadimator / fibonacci_huge.cpp
Created July 19, 2016 05:49
Huge Fibonacci Number modulo m
#include <iostream>
long long get_pisano_period(long long m) {
long long a = 0, b = 1, c = a + b;
for (int i = 0; i < m * m; i++) {
c = (a + b) % m;
a = b;
b = c;
if (a == 0 && b == 1) return i + 1;
}
@aadimator
aadimator / fibonacci_last_digit.cpp
Created July 19, 2016 05:50
The Last Digit of a Large Fibonacci Number
#include <iostream>
int get_fibonacci_last_digit(long long n) {
int first = 0;
int second = 1;
int res;
for (int i = 2; i <= n; i++) {
res = (first + second) % 10;
@aadimator
aadimator / gcd.cpp
Created July 19, 2016 05:51
Greatest Common Divisor
#include <iostream>
int gcd(int a, int b) {
int current_gcd = 1;
for (int d = 2; d <= a && d <= b; d++) {
if (a % d == 0 && b % d == 0) {
if (d > current_gcd) {
current_gcd = d;
}
}
@aadimator
aadimator / lcm.cpp
Created July 19, 2016 05:52
Least Common Multiple
#include <iostream>
int euclidGCD (long a, long b) {
if (b == 0) return a;
return euclidGCD(b, a%b);
}
long long lcm(long long a, long long b) {
return (a * b)/euclidGCD(a, b);
@aadimator
aadimator / change.cpp
Created July 25, 2016 02:48
Changing Money
#include <iostream>
int get_change(int n) {
int coins[] = {10, 5, 1};
int min = 0;
for (int i = 0; n > 0; i++) {
min += n / coins[i];
n %= coins[i];
}
@aadimator
aadimator / dot_product.cpp
Created July 25, 2016 02:49
Minimum Dot Product
#include <algorithm>
#include <iostream>
using std::vector;
long long min_dot_product(vector<int> a, vector<int> b) {
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end(), std::greater<int>());
long long result = 0;
for (int i = 0; i < a.size(); i++) {