I've just written code for generating all permutations of the numbers from 1 to n in Java. It seems to work, but I think it's a bit more complex than it needs to be.
import java.util.*;
public class ProblemFour {
private static int n;
private static void printResult(int[] result) {
Set<Integer> set = new HashSet<>();
// I am using Hashset<> to avoid repetitions
Integer[] nums = new Integer[result.length];
for (int i = 0; i < n; i++) {
nums[i] = result[i];
}
Collections.addAll(set, nums);
if(set.size() == n) System.out.println(Arrays.toString(nums));
}
private static void permute(int[] result, int index) {
if (index == result.length) {
printResult(result);
return;
// Recursion bottom
}
for (int i = 1; i <= n; i++) {
result[index] = i;
permute(result, index+1);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("From 1 to: ");
n = input.nextInt();
int[] result = new int[n];
permute(result, 0);
}
}