Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
53
Task/Factorial/Java/factorial-1.java
Normal file
53
Task/Factorial/Java/factorial-1.java
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package programas;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class IterativeFactorial {
|
||||
|
||||
public BigInteger factorial(BigInteger n) {
|
||||
if ( n == null ) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
else if ( n.signum() == - 1 ) {
|
||||
// negative
|
||||
throw new IllegalArgumentException("Argument must be a non-negative integer");
|
||||
}
|
||||
else {
|
||||
BigInteger factorial = BigInteger.ONE;
|
||||
for ( BigInteger i = BigInteger.ONE; i.compareTo(n) < 1; i = i.add(BigInteger.ONE) ) {
|
||||
factorial = factorial.multiply(i);
|
||||
}
|
||||
return factorial;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
BigInteger number, result;
|
||||
boolean error = false;
|
||||
System.out.println("FACTORIAL OF A NUMBER");
|
||||
do {
|
||||
System.out.println("Enter a number:");
|
||||
try {
|
||||
number = scanner.nextBigInteger();
|
||||
result = new IterativeFactorial().factorial(number);
|
||||
error = false;
|
||||
System.out.println("Factorial of " + number + ": " + result);
|
||||
}
|
||||
catch ( InputMismatchException e ) {
|
||||
error = true;
|
||||
scanner.nextLine();
|
||||
}
|
||||
|
||||
catch ( IllegalArgumentException e ) {
|
||||
error = true;
|
||||
scanner.nextLine();
|
||||
}
|
||||
}
|
||||
while ( error );
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
}
|
||||
56
Task/Factorial/Java/factorial-2.java
Normal file
56
Task/Factorial/Java/factorial-2.java
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package programas;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class RecursiveFactorial {
|
||||
|
||||
public BigInteger factorial(BigInteger n) {
|
||||
if ( n == null ) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
else if ( n.equals(BigInteger.ZERO) ) {
|
||||
return BigInteger.ONE;
|
||||
}
|
||||
else if ( n.signum() == - 1 ) {
|
||||
// negative
|
||||
throw new IllegalArgumentException("Argument must be a non-negative integer");
|
||||
}
|
||||
else {
|
||||
return n.equals(BigInteger.ONE)
|
||||
? BigInteger.ONE
|
||||
: factorial(n.subtract(BigInteger.ONE)).multiply(n);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
BigInteger number, result;
|
||||
boolean error = false;
|
||||
System.out.println("FACTORIAL OF A NUMBER");
|
||||
do {
|
||||
System.out.println("Enter a number:");
|
||||
try {
|
||||
number = scanner.nextBigInteger();
|
||||
result = new RecursiveFactorial().factorial(number);
|
||||
error = false;
|
||||
System.out.println("Factorial of " + number + ": " + result);
|
||||
}
|
||||
catch ( InputMismatchException e ) {
|
||||
error = true;
|
||||
scanner.nextLine();
|
||||
}
|
||||
|
||||
catch ( IllegalArgumentException e ) {
|
||||
error = true;
|
||||
scanner.nextLine();
|
||||
}
|
||||
}
|
||||
while ( error );
|
||||
scanner.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
37
Task/Factorial/Java/factorial-3.java
Normal file
37
Task/Factorial/Java/factorial-3.java
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import java.math.BigInteger;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class LargeFactorial {
|
||||
public static long userInput;
|
||||
public static void main(String[]args){
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.println("Input factorial integer base: ");
|
||||
try {
|
||||
userInput = input.nextLong();
|
||||
System.out.println(userInput + "! is\n" + factorial(userInput) + " using standard factorial method.");
|
||||
System.out.println(userInput + "! is\n" + factorialRec(userInput) + " using recursion method.");
|
||||
}catch(InputMismatchException x){
|
||||
System.out.println("Please give integral numbers.");
|
||||
}catch(StackOverflowError ex){
|
||||
if(userInput > 0) {
|
||||
System.out.println("Number too big.");
|
||||
}else{
|
||||
System.out.println("Please give non-negative(positive) numbers.");
|
||||
}
|
||||
}finally {
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
public static BigInteger factorialRec(long n){
|
||||
BigInteger result = BigInteger.ONE;
|
||||
return n == 0 ? result : result.multiply(BigInteger.valueOf(n)).multiply(factorial(n-1));
|
||||
}
|
||||
public static BigInteger factorial(long n){
|
||||
BigInteger result = BigInteger.ONE;
|
||||
for(int i = 1; i <= n; i++){
|
||||
result = result.multiply(BigInteger.valueOf(i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue