Skip to content

FibonacciNumber #1157

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

Merged
merged 1 commit into from
Oct 30, 2019
Merged
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
26 changes: 26 additions & 0 deletions Maths/FactorialRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package Maths;

public class FactorialRecursion {

/* Driver Code */
public static void main(String[] args) {
assert factorial(0) == 1;
assert factorial(1) == 1;
assert factorial(2) == 2;
assert factorial(3) == 6;
assert factorial(5) == 120;
}

/**
* Recursive FactorialRecursion Method
*
* @param n The number to factorial
* @return The factorial of the number
*/
public static long factorial(int n) {
if (n < 0) {
throw new IllegalArgumentException("number is negative");
}
return n == 0 || n == 1 ? 1 : n * factorial(n - 1);
}
}
37 changes: 37 additions & 0 deletions Maths/FibonacciNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package Maths;

/**
* Fibonacci: 0 1 1 2 3 5 8 13 21 ...
*/
public class FibonacciNumber {
public static void main(String[] args) {
assert isFibonacciNumber(1);
assert isFibonacciNumber(2);
assert isFibonacciNumber(21);
assert !isFibonacciNumber(9);
assert !isFibonacciNumber(10);
}

/**
* Check if a number is perfect square number
*
* @param number the number to be checked
* @return <tt>true</tt> if {@code number} is perfect square, otherwise <tt>false</tt>
*/
public static boolean isPerfectSquare(int number) {
int sqrt = (int) Math.sqrt(number);
return sqrt * sqrt == number;
}

/**
* Check if a number is fibonacci number
* This is true if and only if at least one of 5x^2+4 or 5x^2-4 is a perfect square
*
* @param number the number
* @return <tt>true</tt> if {@code number} is fibonacci number, otherwise <tt>false</tt>
* @link https://en.wikipedia.org/wiki/Fibonacci_number#Identification
*/
public static boolean isFibonacciNumber(int number) {
return isPerfectSquare(5 * number * number + 4) || isPerfectSquare(5 * number * number - 4);
}
}
16 changes: 5 additions & 11 deletions Others/CountChar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

import java.util.Scanner;


/**
* @author blast314
* <p>
* Counts the number of characters in the text.
*/

public class CountChar {

public static void main(String[] args) {
Expand All @@ -20,11 +13,12 @@ public static void main(String[] args) {
}

/**
* @param str: String to count the characters
* @return int: Number of characters in the passed string
* Count non space character in string
*
* @param str String to count the characters
* @return number of character in the specified string
*/
private static int CountCharacters(String str) {
str = str.replaceAll("\\s","");
return str.length();
return str.replaceAll("\\s", "").length();
}
}
50 changes: 0 additions & 50 deletions Others/Factorial.java

This file was deleted.