Sep 11, 2024 Groups & Rings
Determine if a set of integers forms a mathematical group or ring based on their respective properties. Groups
import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
public class Group {
private Set<Integer> elements;
public Group(Set<Integer> elements) {
this.elements = elements;
}
private boolean contains(int element) {
return elements.contains(element);
}
public boolean isClosedUnderOperation() {
for (int a : elements) {
for (int b : elements) {
if (!contains(a + b)) {
return false;
}
}
}
return true;
}
public boolean isAssociative() {
for (int a : elements) {
for (int b : elements) {
for (int c : elements) {
if ((a + (b + c)) != ((a + b) + c)) {
return false;
}
}
}
}
return true;
}
public Integer getIdentityElement() {
for (int e : elements) {
boolean isIdentity = true;
for (int a : elements) {
if ((e + a) != a || (a + e) != a) {
isIdentity = false;
break;
}
}
if (isIdentity) {
return e;
}
}
return null;
}
public boolean hasInverseElement() {
Integer identity = getIdentityElement();
if (identity == null) {
return false;
}
for (int a : elements) {
boolean hasInverse = false;
for (int b : elements) {
if ((a + b) == identity) {
hasInverse = true;
break;
}
}
if (!hasInverse) {
return false;
}
}
return true;
}
public boolean isGroup() {
return isClosedUnderOperation() && isAssociative() && getIdentityElement() != null && hasInverseElement();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Set<Integer> elements = new HashSet<>();
System.out.println("Enter the elements of the group (type 'done' to finish):");
while (scanner.hasNextInt()) {
elements.add(scanner.nextInt());
}
scanner.next(); // To consume the 'done' or invalid input
Group group = new Group(elements);
if (group.isGroup()) {
System.out.println("The set forms a group under addition.");
} else {
System.out.println("The set does not form a group under addition.");
}
scanner.close();
}
}
Enter the elements of the group (type 'done' to finish):
0
1
-1
2
-2
done
The set does not form a group under addition.
Rings
import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
public class Ring {
private Set<Integer> elements;
public Ring(Set<Integer> elements) {
this.elements = elements;
}
private boolean contains(int element) {
return elements.contains(element);
}
public boolean isClosedUnderAddition() {
for (int a : elements) {
for (int b : elements) {
if (!contains(a + b)) {
return false;
}
}
}
return true;
}
public boolean isClosedUnderMultiplication() {
for (int a : elements) {
for (int b : elements) {
if (!contains(a * b)) {
return false;
}
}
}
return true;
}
public boolean isAssociativeAddition() {
for (int a : elements) {
for (int b : elements) {
for (int c : elements) {
if ((a + (b + c)) != ((a + b) + c)) {
return false;
}
}
}
}
return true;
}
public boolean isAssociativeMultiplication() {
for (int a : elements) {
for (int b : elements) {
for (int c : elements) {
if ((a * (b * c)) != ((a * b) * c)) {
return false;
}
}
}
}
return true;
}
public boolean isDistributive() {
for (int a : elements) {
for (int b : elements) {
for (int c : elements) {
if ((a * (b + c)) != ((a * b) + (a * c))) {
return false;
}
}
}
}
return true;
}
public Integer getAdditiveIdentity() {
for (int e : elements) {
boolean isIdentity = true;
for (int a : elements) {
if ((e + a) != a || (a + e) != a) {
isIdentity = false;
break;
}
}
if (isIdentity) {
return e;
}
}
return null;
}
public boolean isRing() {
return isClosedUnderAddition() && isClosedUnderMultiplication() &&
isAssociativeAddition() && isAssociativeMultiplication() &&
isDistributive() && getAdditiveIdentity() != null;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Set<Integer> elements = new HashSet<>();
System.out.println("Enter the elements of the ring (type 'done' to finish):");
while (scanner.hasNextInt()) {
elements.add(scanner.nextInt());
}
scanner.next(); // To consume the 'done' or invalid input
Ring ring = new Ring(elements);
if (ring.isRing()) {
System.out.println("The set forms a ring.");
} else {
System.out.println("The set does not form a ring.");
}
scanner.close();
}
}
Enter the elements of the ring (type 'done' to finish):
0
1
-1
2
-2
done
The set does not form a ring.