Te dejo una clase que estoy haciendo recién para operaciones matemáticas:
package org.educacode.mathio;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Math util methods and classes
* @author Gustavo Garsaky
* @version 0.0.1
*/
public class MathIO {
public final static PrimeNumbers PRIME_NUMBERS = new PrimeNumbers();
public final static class PrimeNumbers {
public List<Integer> listPrimesUntil(int limit) {
List<Integer> naturals = new CopyOnWriteArrayList<>();
// init naturals
for(int i=2; i<=limit; i++) {
naturals.add(i);
}
discardMultiples(naturals, getMultiplesOf(current, naturals.size()));
// if discard continue, that means the process should continue
if(discard(current, naturals.size())) {
continue;
} else {
break;
}
}
List<Integer> primes = naturals;
return primes;
}
/**
* check the first natural quad. If it's
* less tan limit, process should continue
* @param firstNatural first natural number
*/
private boolean discard(int firstNatural, int limit) {
double firstNaturalQuad
= Math.
pow(new Double(String.
valueOf(firstNatural
)), 2.0d
); if(firstNaturalQuad <= limit) {
return true;
}
return false;
}
/**
* Get new list of natural numbers. The new list have all numbers
* that not in multiples of first natural number
* @param naturals Naturals numbers
* @param number Number to get multiples
*/
private void discardMultiples(List<Integer> naturals, List<Integer> multiples) {
// check if each multiple is in naturals list.
// if it isn't, append the multiple to new list
for(int i=0; i<multiples.size(); i++) {
for(int k=0; k<naturals.size(); k++) {
if(multiples.get(i) == naturals.get(k)) {
naturals.remove(k);
}
}
}
}
} // end prime numbers class
/**
* Get the multiples of a number with some limit
* @param number Number to get multiples
* @param limit of multiples to get
*/
public static List<Integer> getMultiplesOf(int number, int limit) {
List<Integer> multiples = new ArrayList<>();
for(int i=number; i<limit; i++ ) {
if(i < limit) {
multiples.add(number * i);
}
}
return multiples;
}
/**
* Show a multiplication table of a number with a limit
* @param number Number to get multiplication table
* @param limit Limit of multiplicator
*/
public static void showMultipTableOf(int number, int limit) {
for(int i=1; i<=limit; i++) {
System.
out.
println(number
+ " x " + i
+ " = " + (number
* i
)); }
}
}
De momento saca los números primos de 2 a n-1 y muestra la tabla de multiplicar de un número a n, donde n es el límite del multiplicador.
Por ejemplo, en tu caso, solo haría:
import java.util.Scanner;
import org.educacode.mathio.MathIO;
public class MultiplicationTableTest {
public static void main
(String[] args
) { final Scanner READER
= new Scanner
(System.
in); int initial;
int last;
int limit;
System.
out.
println("**** Tabla de multiplicar ****\n\n"); System.
out.
println("[+] Ingresa en rango los números"); initial = READER.nextInt();
last = READER.nextInt();
System.
out.
println("[+] Ingrese el límite del multiplicador"); limit = READER.nextInt();
READER.close();
for(int i = initial; i <= last; i++) {
MathIO.showMultipTableOf(i, limit);
}
}
}
Y te mostraría las tablas de multiplicar de un rango dado.