Skip to content

Instantly share code, notes, and snippets.

@seraekim
Created November 28, 2017 11:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seraekim/24d09682a7954a739f83898ddb4e678f to your computer and use it in GitHub Desktop.
Save seraekim/24d09682a7954a739f83898ddb4e678f to your computer and use it in GitHub Desktop.
let's get multiple coprime with combinations and divisors.
package org.srkim.test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* let's get multiple coprime with combinations and divisors.
*
* @author srkim
*
*/
public class MultiCoprime {
private static List<List<Integer>> list = new ArrayList<List<Integer>>();
private static int[] nums = { 3, 5, 6, 9, 11 };
public static void main(String[] args) {
int size = nums.length;
for (int i = 0; i < size; i++) {
getDivisors(nums[i]);
}
for (int i = 2; i <= size; i++) {
combination(new int[size], 0, size, i, 0);
}
}
public static void getDivisors(int num) {
// add divisors except 1
List<Integer> result = new ArrayList<Integer>();
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
result.add(i);
}
}
result.add(num);
list.add(result);
}
public static void multiCoprime(int[] arr, int length) {
int totalSize = 0;
Set<Integer> setList = new HashSet<Integer>();
for (int i = 0; i < length; i++) {
List<Integer> imsiList = list.get(arr[i]);
totalSize += imsiList.size();
System.out.println(imsiList.get(imsiList.size() - 1) + "'s set : " + imsiList);
setList.addAll(imsiList);
}
for (int i = 0; i < length; i++)
System.out.print(nums[arr[i]] + " ");
if (totalSize == setList.size()) {
System.out.println("==> coprime");
} else {
System.out.println("==> not coprime");
}
System.out.println();
}
public static void combination(int[] arr, int index, int n, int r, int target) {
if (r == 0)
multiCoprime(arr, index);
else if (target == n)
return;
else {
arr[index] = target;
combination(arr, index + 1, n, r - 1, target + 1);
combination(arr, index, n, r, target + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment