Methods Explained - VICamaraPrg/NewMath GitHub Wiki
Note: All methods are made to be as efficient as possible, either iterative or recursive. With time, some methods will be improved in therms of complexity, both time and computing.
Version: 1.0
This method returns a boolean
based if the input is an abundant number.
An abundant number is a number that the sum of its natural factors (Excluding itself), is higher than the input itself.
For example: 12 is the first abundant number, because the sum of its natural factors is higher than 12 [1,2,3,4,6] = 16
.
Version: 1.0
This method returns a boolean
based if the input is a deficient number.
A deficient number is the oposite of abundant number, the sum of its natural factors (Excluding itself), is lower than the input itself.
For example: 8 is a deficient number, because the sum of its natural factors is lower than 8 [1,2,4] = 7
.
Version: 1.0
This method returns a boolean
based if the input is a perfect number.
A perfect number stands inbetween of abundant number and deficient number, the sum of its natural factors (Excluding itself), is exacly the same to the input itself.
For example: 6 and 28 are the first of the first hundred natural perfect numbers, because the sum of its natural factors are equal to themselves respectively:
(6) [1,2,3] = 6 | (28) [1,2,4,7,14] = 28
.
Version: 1.0
This method returns an ArrayList<Integer>
with all the natural factors/divisors of the input.
As a difference from prime factorization, this method will return every single possible factor of the input.
For example: (14827579) [1, 13, 31, 403, 36793, 478309, 1140583, 14827579]
.
Version: 1.0
Computing complexity:
sqrt(x) / 2
, already tested.
This method returns an ArrayList<Integer>
with all the prime factors of the input.
For example: (2048) = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
or 2^11.(3219109) = [43, 43, 1741]
or (43^2 * 1741).
Version: 1.0
This method takes as a parameter whatever int
value positive or negative, and returns the next closest prime number to that input number.
So if our input is 9
, the output will be the next prime closest to 9, so that is 11
. As well as if our input is 23
(A prime number itself), will return the next one: 29
.
Version: 1.0
This method takes as a parameter a number n
and evaluate it to see if is it a prime number, returns true
if is a prime, false
if not.
A prime number a natural number, that its only divisors are 1 and itself. Because only natural numbers can be prime numbers, an ArithmeticException
is thrown if the input is a negative number.
A little optimization can be made, since there's no even prime numbers except 2, we can skip even numbers if it's not 2. Planned for a future release.
Version: 1.0
This method takes as a parameter the desired limit to get prime numbers, returning them as ArrayList<Integer>
.
For example if our input is 46, will return: [0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]
.
As this method calls directly to isPrime(), optimizing isPrime() will also optimize this method as well.
Fraction class has 2 different constructors, one indicating a double
value that will perform the transformation to a readable fraction.
For example, if we input 5.2181756
, our fraction will be 52181756/10000000
, hopefully we have a method to simplify fractions, explained after constructors!!.
The second constructor takes as a parameter two int
values, indicating the numerator and the denominator.
If we input 13045439/2500000
, our fraction value will be 5.2181756
, that is the simplified fraction of the previous example!!
Version: 1.0
This method reduces the fraction to the lowests numbers possible, it does it by applying the greatestCD of the numerator and denominator.
So if we have the following fraction: 1024/2048
, the simplified way to express it is 1/2
!! Or 0.5
as value.
This class has 4 getters: getDenominator(), getNumerator(), getValue() and toString().
This class has 2 setters: setDenominator(), setNumerator(). There's no point to modify the actual value of a fraction, since it will modify denominator and numerator.
Version: 1.0
This method takes as a parameter the desired limit of the Fibonacci sequence as an ArrayList<Long>
.
Fibonacci sequence takes 2 starting numbers (Usually 0 and 1), and the sequence follows as: The next number is the sum of the 2 previous ones
.
Or n+1 = ((n) + (n-1))
.
For example: If limit is 19, the method will output: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
That is, the first 19 terms of the Fibonacci sequence expressed as long
values, since int
values are pretty short to handle this sequence.
That's why an StackOverflowError
is thrown if the limit of the sequence is higher than 91, because it overflows 9223372036854775807
.
We take note that it hasn't necesarily to return a long
, since we can express it in scientific notation.
Anyway this feature is planned for a future release.
Version: 1.0
This method takes as a parameter a number n
to make the factorial sequence from 0 to n
, and returns it as an ArrayList<Long>
.
The factorial is the product of all positive integers less or equal than n
, or n! = n(n-1)(n-2)...
also 7! = 7·6·5·4·3·2·1 = 5040
.
As the output of 7!
in the method will be: [1, 2, 6, 24, 120, 720, 5040]
.
We have the same problem as Fibonacci sequence, as the factorial grows even faster than Fiboncci, the max limit to express it cannot be higher than 20 or an StackOverflowError
will be thrown.
We take note that it hasn't necesarily to return a long
, since we can express it in scientific notation.
Anyway this feature is planned for a future release.
Version: 1.0
Note: This method has already an overcharge, accepting
int
andlong
values.
This method takes as a parameter 2 integers and outputs the greatest common divisor as int
.
The greatest common divisor applies the commutative property, so gcd(100,42) == gcd(42,100)
.
The implementation of this method is based on Euclid Division
algorithm that stands on the next formula rk = (rk-2) % (rk-1)
, where r
is the remainder, and k
the state of iteration.
So we need to iterate the formula and do a variable exchange to get close to the result.
For example: We want to calculate greatestCD(204, 165)
> a = 204, b = 165, temp = 165
> b = (204 % 165) = 39, a = 165
> temp = 39, b = (165 % 39) = 9, a = 39
> temp = 9, b = (39 % 9) = 3, a = 9
> temp = 3, b = (9 % 3) = 0, a = 3
.
The method is set to do this until b
is different of 0, on that last iteration, the last state of a
was 3, so the loop cannot continue anymore and a
is returned, greatestCD(204, 165) = 3.
Version: 1.0
Note: This method has already an overcharge, accepting
int
andlong
values.
This method takes as a parameter 2 integers and outputs the least common multiple as int
.
As for the algorithm, I used the given formula which also uses greatestCD(): leastCM(a,b) = (|(a*b)| / greatestCD(a,b))
So: |3*12| / greatestCD(3,8) = 36 / 3 = 12
.
Version: 1.0
This method returns a long
value indicating what is the input factorial.
For example: If our input is 11
, will return; (1*2*3*4*5*6*7*8*9*10*11) = 39916800
.
Version: 1.0
This method uses the previous factorial method to generate a list with all factorial numbers from 1 to the input number (inclusive).
For example: If our input is 9
, the method will return an ArrayList<Long>
, containing [1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
.
That is, the method does this: [1!, 2!, 3!, 4!, 5!, 6!, 7!, 8!, 9!]
Version: 1.0
This method returns the double factorial (or semi factorial) of a number as long
value.
Don't misunderstand double factorial as (n!)!
, as that means the factorial of the factorial or (5!)! = 120! = A huge number
.
Double factorial takes the parity (odd or even) as a crucial step to calculate it, that means if the input is an even number, will take its lower even natural numbers (including itself, but excluding 0) and multiply it by them or: 6!! = 6*4*2 = 48
And if it is an odd number: 7!! = 7*5*3*1 = 105
.