Last active
September 28, 2015 00:48
-
-
Save tssm/1358660 to your computer and use it in GitHub Desktop.
Some algorithms of recreational mathematics that I did as exercises in my first (and only) semester studying Computer Science.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int getFactorialOf(unsigned number) { | |
if (number == 0) { | |
return 1; | |
} | |
unsigned int factorial, i; | |
for (factorial = i = 1; i <= number; factorial *= i, i++); | |
return factorial; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isNarcissistic(number): | |
# Determines the number of digits: | |
digits = 0 | |
for (dividend = number; dividend > 0; dividend //= 10): | |
digits += 1 | |
# Calculates the sum of the squared digits: | |
digitsSum = 0 | |
for (dividend = number; dividend > 0; dividend //= 10): | |
digitsSum += (dividend % 10) ** digits | |
# Returns the answer: | |
if (digitsSum == number): | |
return true | |
return false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool isHappy(unsigned int number) { | |
unsigned int sum; | |
do { | |
sum = 0; | |
for (; number > 0; sum += (number % 10) * (number % 10), number /= 10); | |
if (sum < 10) break; | |
number = sum; | |
} while (true); | |
if (sum == 1) return true; | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool isRepunit(number) { | |
while (number > 0) { | |
if (number % 10 != 1) return false; | |
number /= 10; | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool isPrime(long int number) { | |
if (number <= 1) return false; | |
if (number == 2) return true; | |
for (unsigned int i = 2; i < (number / 2) + 1; ++i) { | |
if (number % i == 0) return false; | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool isAutomorphic(number) { | |
unsigned int digits = 0, divisor = 1, squareNumber = number * number; | |
// Calculate the number of digits: | |
for (unsigned int i = number; i > 0; i /= 10, ++digits); | |
// Calculate the divisor to use: | |
for (int i = 1; i <= digits; divisor *= 10, i++); | |
if ((squareNumber % divisor) == number) return true; | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool isPalindromic(unsigned int number) { | |
int reversed = 0; | |
// Reverse the number: | |
for (unsigned int i = number; i > 0; reversed = (10 * reversed) + (i % 10), i /= 10); | |
if (reversed == number) return true; | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool isPerfect(unsigned int number) { | |
unsigned int divisorsSum = 1; | |
for (divisor = 2; divisor <= (number / 2) + 1; ++divisor) { | |
if (number % divisor == 0) { | |
divisorsSum += divisor; | |
} | |
} | |
if (number == divisorsSum) { | |
return true; | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Change the base of a decimal base integer | |
unsigned int(unsigned int number, unsigned short int newBase) { | |
unsigned int baseChanged = 0, exponent = 1; | |
for (unsigned int quotient = number; quotient > 0; quotient /= base) { | |
baseChanged += exponent * (quotient % base); | |
exponent = (10 * exponent); | |
} | |
return baseChanged; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment