Package net.n1da.dev.euler.helper
Class Mathe
- java.lang.Object
-
- net.n1da.dev.euler.helper.Mathe
-
public class Mathe extends java.lang.Object
Helper class for all mathematical methods. Using strange name due to name conflicts withMath
.- Since:
- 25.05.2015 07:09:19
- Author:
- Nico Danneberg
-
-
Field Summary
Fields Modifier and Type Field Description private static int[]
powers2
Stores all powers of 2 to save some calculation time.
-
Constructor Summary
Constructors Modifier Constructor Description private
Mathe()
No instances wanted!
-
Method Summary
Modifier and Type Method Description static int
getAlphabeticalValue(java.lang.String word)
Calculates the alphabetical value of the given word by summing the byte values of every character minus byte value of 'A' plus one.static long
getCountOfDivisors(long num)
Calculates the count of all proper divisors for the given number.static long[]
getDivisors(long num)
Finds all proper divisors of the given number.static int
getGCD(int a, int b)
Calculates the Greatest Common Divisor (GCD) using Euclid's algorithm.static int
getHeptagonalNumber(int n)
static int
getHexagonalNumber(int n)
static int
getLength(int x)
Reads and returns the number of digits from the given number.static int
getOctagonalNumber(int n)
static int
getPentagonalNumber(int n)
static long[]
getPrimeFactors(long num)
Finds all prime factors of the given number.static int
getSquareNumber(int n)
static long
getSumOfDivisors(long num)
Calculates the sum of all proper divisors for the given number.static int
getTriangleNumber(int n)
static boolean
isAbundantNumber(int num)
Checks if the given number is a abundant number.static boolean
isDeficientNumber(int num)
Checks if the given number is a deficient number.static boolean
isPalindrome(int x)
Checks the given integer to be a palindrome.static boolean
isPalindrome(java.lang.String s)
Checks the given string to be a palindrome.static boolean
isPandigital(java.lang.String num)
Checks if the numbers from 1 to 9 exist exactly once as digits in the given number.static boolean
isPandigital(java.lang.String num, int n)
Checks if the numbers from 1 to n exist exactly once as digits in the given number.static boolean
isPentagonNumber(int num)
Checks if the given number is a pentagon number.static boolean
isPentagonNumber(long num)
Checks if the given number is a pentagon number.static boolean
isPerfectNumber(int num)
Checks if the given number is a perfect number.static boolean
isPrime(int num)
Checks a given number to be a prime.static boolean
isPythagoreanTriplet(int a, int b, int c)
Checks if the given three number are a Pythagorean triplet.static boolean
isTriangleNumber(int num)
Checks if the given number is a triangle number.static int
rotate(int x)
Lets the given number rotating by cutting the last digit and moving it to the very first position, e.g. 1234 rotates to 4123.static int
rotate(int x, int len)
Lets the given number rotating by cutting the last digit and moving it to the very first position, e.g. 1234 rotates to 4123.static int[]
split(int x, int min)
Splits the given integer number into an array of all its digits.
-
-
-
Method Detail
-
getAlphabeticalValue
public static int getAlphabeticalValue(java.lang.String word)
Calculates the alphabetical value of the given word by summing the byte values of every character minus byte value of 'A' plus one.- Parameters:
word
- the word to calculate the alphabetical value from- Returns:
- the alphabetical value of the given name
-
getCountOfDivisors
public static long getCountOfDivisors(long num)
Calculates the count of all proper divisors for the given number.- Parameters:
num
- the number whose divisors are wanted- Returns:
- the count of all proper divisors
- See Also:
getDivisors(long)
-
getDivisors
public static long[] getDivisors(long num)
Finds all proper divisors of the given number.- Parameters:
num
- the number whose divisors are wanted- Returns:
- an array of all proper divisors
-
getGCD
public static int getGCD(int a, int b)
Calculates the Greatest Common Divisor (GCD) using Euclid's algorithm.- Parameters:
a
- the 1st number with some divisorsb
- the 2nd number with some divisors- Returns:
- the Greatest Common Divisor of both given numbers
-
getHeptagonalNumber
public static int getHeptagonalNumber(int n)
-
getHexagonalNumber
public static int getHexagonalNumber(int n)
-
getLength
public static int getLength(int x)
Reads and returns the number of digits from the given number. If the number is less than zero the "-" character is ignored.- Parameters:
x
- the number to be tested- Returns:
- the number of digits in the given number
-
getOctagonalNumber
public static int getOctagonalNumber(int n)
-
getPentagonalNumber
public static int getPentagonalNumber(int n)
-
getPrimeFactors
public static long[] getPrimeFactors(long num)
Finds all prime factors of the given number.- Parameters:
num
- the number whose prime factors are wanted- Returns:
- an array of all prime factors
-
getSquareNumber
public static int getSquareNumber(int n)
-
getSumOfDivisors
public static long getSumOfDivisors(long num)
Calculates the sum of all proper divisors for the given number.- Parameters:
num
- the number whose divisors are wanted- Returns:
- the sum of all proper divisors
- See Also:
getDivisors(long)
-
getTriangleNumber
public static int getTriangleNumber(int n)
-
isAbundantNumber
public static boolean isAbundantNumber(int num)
Checks if the given number is a abundant number.- Parameters:
num
- the number to be checked- Returns:
- true if the given number is abundant
- See Also:
getSumOfDivisors(long)
, Abundant number @ Wikipedia
-
isDeficientNumber
public static boolean isDeficientNumber(int num)
Checks if the given number is a deficient number.- Parameters:
num
- the number to be checked- Returns:
- true if the given number is deficient
- See Also:
getSumOfDivisors(long)
, Deficient number @ Wikipedia
-
isPalindrome
public static boolean isPalindrome(int x)
Checks the given integer to be a palindrome.- Parameters:
x
- the number to be checked- Returns:
- true, if the number is a palindrome
- See Also:
- Palindromic number @ Wikipedia
-
isPalindrome
public static boolean isPalindrome(java.lang.String s)
Checks the given string to be a palindrome.- Parameters:
s
- the string to be checked- Returns:
- true, if the string is a palindrome
- See Also:
- Palindromic number @ Wikipedia
-
isPandigital
public static boolean isPandigital(java.lang.String num)
Checks if the numbers from 1 to 9 exist exactly once as digits in the given number.- Parameters:
num
- number to be checked- Returns:
- true, if the given number is pandigital
- See Also:
isPandigital(String, int)
-
isPandigital
public static boolean isPandigital(java.lang.String num, int n)
Checks if the numbers from 1 to n exist exactly once as digits in the given number.- Parameters:
num
- number to be checkedn
- the maximum digit of the number- Returns:
- true, if the given number is pandigital
- See Also:
isPandigital(String)
-
isPentagonNumber
public static boolean isPentagonNumber(int num)
Checks if the given number is a pentagon number. It uses a transformed version of the formulap = 0.5 * n * ( 3 * n - 1 )
to check this quickly.- Parameters:
num
- the number to be checked- Returns:
- true, if an integer n can be calculated out of the given number
-
isPentagonNumber
public static boolean isPentagonNumber(long num)
Checks if the given number is a pentagon number. It uses a transformed version of the formulap = 0.5 * n * ( 3 * n - 1 )
to check this quickly.- Parameters:
num
- the number to be checked- Returns:
- true, if an integer n can be calculated out of the given number
-
isPerfectNumber
public static boolean isPerfectNumber(int num)
Checks if the given number is a perfect number.- Parameters:
num
- the number to be checked- Returns:
- true if the given number is perfect
- See Also:
getSumOfDivisors(long)
, Perfect number @ Wikipedia
-
isPrime
public static boolean isPrime(int num)
Checks a given number to be a prime.- Parameters:
num
- the number to be checked- Returns:
- true if the given number is a prime
-
isPythagoreanTriplet
public static boolean isPythagoreanTriplet(int a, int b, int c)
Checks if the given three number are a Pythagorean triplet.- Parameters:
a
- the well-known a in the Pythagorean equationb
- the well-known b in the Pythagorean equationc
- the well-known c in the Pythagorean equation- Returns:
- true, if a² + b² = c²
- See Also:
- Pythagorean triple @ Wikipedia
-
isTriangleNumber
public static boolean isTriangleNumber(int num)
Checks if the given number is a triangle number. It uses a transformed version of the formulat = 0.5 * n * ( n + 1 )
to check this quickly.- Parameters:
num
- the number to be checked- Returns:
- true, if an integer n can be calculated out of the given number
-
rotate
public static int rotate(int x)
Lets the given number rotating by cutting the last digit and moving it to the very first position, e.g. 1234 rotates to 4123.- Parameters:
x
- the number to be rotated- Returns:
- a new number with the right-most digit on the left side
- See Also:
rotate(int, int)
-
rotate
public static int rotate(int x, int len)
Lets the given number rotating by cutting the last digit and moving it to the very first position, e.g. 1234 rotates to 4123. In this version the length is given as second parameter. This saves much time for calculating the length.- Parameters:
x
- the number to be rotatedlen
- the length of x- Returns:
- a new number with the right-most digit on the left side
- See Also:
rotate(int)
-
split
public static int[] split(int x, int min)
Splits the given integer number into an array of all its digits.- Parameters:
x
- the number to be splitmin
- a minimum length the returned array must have- Returns:
- an array of the split number
-
-