Your task is to generate all possible functions from X={a, b, c} to a set Y. Set Y contains integers 1, …, n for some integer n≥1. The value of n is provided by the user. To define a function you need to specify what it outputs for each element of X. For instance, if Y= {1, 2}, then f(a)=1, f(b)=2, f(c)=2 defines function f from X to Y
The value of n is provided by the user. To define a function you need to specify what it outputs for each element of X. For instance, if Y= {1, 2}, then f(a)=1, f(b)=2, f(c)=2 defines function f from X to Y.
Write a program that prompts the user to enter the size of Y, then generates, enumerates, and prints out in a neat format all possible functions from X to Y. Your program should number generated functions f1, f2, f3, f4, etc. For each generated function, output whether or not it is one-to-one, onto, or a bijection. Compute total number of functions generated, how many of them are one-to-one, how many of them are onto, and how many of them are bijections.
The program will generate all functions from X={a,b,c} to Y={1,…,n}.
Please enter the value of n: 2
f1(a)=1 f1(b)=1 f1(c)=1
f1 is not one-to-one, not onto, and not a bijection
f2(a)=1 f2(b)=1 f2(c)=2
f2 is not one-to-one, onto, and not a bijection.
f3(a)=1 f3(b)=2 f3(c)=1
f3 is not one-to-one, onto, and not a bijection.
f4(a)=1 f4(b)=2 f4(c)=2
f4 is not one-to-one, onto, and not a bijection.
f5(a)=2 f5(b)=1 f5(c)=1
f5 is not one-to-one, onto, and not a bijection.
f6(a)=2 f6(b)=1 f6(c)=2
f6 is not one-to-one, onto, and not a bijection.
f7(a)=2 f7(b)=2 f7(c)=1
f7 is not one-to-one, onto, and not a bijection.
f8(a)=2 f8(b)=2 f8(c)=2
f8 is not one-to-one, not onto, and not a bijection.
There are 8 functions total.
0 of them are one-to-one.
6 of them are onto.
0 of them are bijections.
public class Main {
public static void main(String[] args) {
Set<Integer> yGroup = new HashSet<>();
int n;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter value of n:");
n = Integer.parseInt(scanner.nextLine());
scanner.close();
for (int i = 1; i <= n; i++) {
yGroup.add(i);
}
int nf = 1;
int oneToneCounter = 0;
int ontoCounter = 0;
int bijectCounter = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
System.out.print("f" + nf + "(a)=" + i + " ");
System.out.print("f" + nf + "(b)=" + j + " ");
System.out.print("f" + nf + "(c)=" + k + " ");
System.out.println();
boolean injective = false;
if ((i == j) || (i == k) || (j == k)) {
System.out.print("f" + nf + " is not a one to one. ");
} else {
System.out.print("f" + nf + " is a one to one. ");
oneToneCounter++;
injective = true;
}
yGroup.remove((Integer) i);
yGroup.remove((Integer) j);
yGroup.remove((Integer) k);
boolean onto = false;
if (yGroup.isEmpty()) {
System.out.print("Is an onto. ");
onto = true;
ontoCounter++;
} else {
System.out.print("Is not an onto. ");
}
yGroup.add((Integer) i);
yGroup.add((Integer) j);
yGroup.add((Integer) k);
if (injective && onto) {
System.out.print("Is bijective. ");
bijectCounter++;
} else {
System.out.print("Is not bijective. ");
}
System.out.println();
System.out.println();
nf++;
}
}
}
System.out.println();
System.out.println("There are " + nf + " functions total");
System.out.println(oneToneCounter + " of them are one-to-one");
System.out.println(ontoCounter + " of them are onto");
System.out.println(bijectCounter + " of them are bijections");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.