Reputation: 65
I have a problem with multiplying numbers.
From my input I get:
Example: if I get from the input 3,2
it means I need to multiply all two digits numbers three times
For now I got some code which works only when I get two numbers with two digits.
How can I implement a method which multiplies as many numbers as user need? I made only this kind of method which prints number of digits got from the input. I can handle method which can multiply them as many times as variable multiplier says...
public static void test(int digits, int multiplier) {
int number = 0;
int result = 0;
ArrayList<String> numbers = new ArrayList<String>();
number = (int) Math.pow(10, digits);
for (int i = (int) (Math.pow(10, digits - 1)); i < number; i++) {
System.out.println(i);
for (int j = (int) (Math.pow(10, digits - 1)); j < number; j++) {
result = j * i;
}
}
System.out.println(number);
}
Sample Input And Output
If number of digits = 2, number of times = 2
We need to multiply all 2 digit numbers 10, 11, 12 .... 99
two times.
10 * 10
10 * 11
.
.
10 * 99
.
.
.
99 * 98
99 * 98
In the same way, if the no of digits is 2, and the no of times is 3
10 * 10 * 10
10 * 10 * 11
.
.
10 * 99 * 99
.
.
.
99 * 99 * 98
99 * 99 * 99
Upvotes: 2
Views: 4067
Reputation: 21608
I would not use recursion, but use a for loop in combination with the Java8 streaming API.
The code example:
import java.util.stream.IntStream;
public class Multiplier {
private final int digits;
public Multiplier(int digits) {
this.digits = digits;
}
/**
* @param multiplificationSteps
* @return a stream, containing all numbers of the given digits, each multiplied several times with each other
*
* digits=1 with multiplificationSteps=1 => output [1;9]
* digits=2 with multiplificationSteps=1 => output [10;99]
* digits=1 with multiplificationSteps=2 => output [1;9] & [2;19] & ... & [9;81]
*/
public IntStream createFullStream(int multiplificationSteps) {
IntStream intStream = makeIntStream();
for (int i = 1; i < multiplificationSteps; i++)
intStream = intStream.flatMap(this::multiplyStream);
return intStream;
}
/**
* @param input a multiplier
* @return the stream, having each number multiplied by the multiplier
*
* input [1;9] and 1 => output [1;9]
* input [1;9] and 2 => output [2;18]
* input [10;99] and 3 => output [30;297]
*/
private IntStream multiplyStream(int input) {
return makeIntStream().map(k -> input * k);
}
/**
* @return a stream of all numbers with the given number of digits
*
* input 1 => output [1;9]
* input 2 => output [10;99]
* input 3 => output [100;999]
*/
private IntStream makeIntStream() {
int startNumber = (int) Math.pow(10, digits - 1);
int endNumber = (int) Math.pow(10, digits);
return IntStream.range(startNumber, endNumber);
}
public static void main(String[] args) {
new Multiplier(1).createFullStream(2).forEach(System.out::println);
}
}
sample input and outputs
For the input 1,1
it outputs:
1
2
3
4
5
6
7
8
9
For 1,2
it outputs:
1
2
3
4
5
6
7
8
9
2
4
6
8
10
12
14
16
18
3
6
9
12
15
18
21
24
27
4
8
12
16
20
24
28
32
36
5
10
15
20
25
30
35
40
45
6
12
18
24
30
36
42
48
54
7
14
21
28
35
42
49
56
63
8
16
24
32
40
48
56
64
72
9
18
27
36
45
54
63
72
81
it also will work without Java8
import java.util.*;
public class PlainOldMultiplier {
private final int digits;
public PlainOldMultiplier(int digits) {
this.digits = digits;
}
public void printAllMultipliedNumbers(int multiplicationSteps) {
// define the result
Collection<Integer> numbers = new ArrayList<>();
numbers.add(1);// multiply by 1 in first iteration
// foreach multiplicationStep once multiply all existing stuff with [start;end[
for (int multiplicationStepCount = 0; multiplicationStepCount < multiplicationSteps; multiplicationStepCount++) {
numbers = multiplyEverything(numbers);
}
// output numbers to console
for (Integer number : numbers) {
System.out.println(number);
}
}
private Collection<Integer> multiplyEverything(Collection<Integer> numbersOfPreviousIteration) {
Collection<Integer> result = new ArrayList<>();
// define start and end by taking account "digits"
int start = (int) Math.pow(10, digits-1);
int end = (int) Math.pow(10, digits);
// foreach number within [start;end[ multiply existing stuff
for (int number = start; number < end; number++) {
for (Integer numberOfPreviousIteration : numbersOfPreviousIteration) {
result.add(numberOfPreviousIteration * number);
}
}
return result;
}
public static void main(String[] args) {
new PlainOldMultiplier(1).printAllMultipliedNumbers(1); // outputs [1;9]
new PlainOldMultiplier(2).printAllMultipliedNumbers(1); // outputs [10;99]
new PlainOldMultiplier(1).printAllMultipliedNumbers(2); // outputs [1;9] & [2;18] & ... & [9;81]
}
}
Upvotes: 1