Open ErSKS opened 5 years ago
package bunkerarray;
/**
@College: Khwopa College of Engineering */ public class BunkerArray {
public static void main(String[] args) { System.out.println("Bunker Array- contains the value 1 if and only if it contains a prime numbers"); System.out.println("{7, 6, 10 ,1}1 :" + isBunker(new int[]{7, 6, 10, 1})); System.out.println("{7, 6, 10 }0 :" + isBunker(new int[]{7, 6, 10})); System.out.println("{1, 6, 10 }0 :" + isBunker(new int[]{1, 6, 10})); System.out.println("{3, 7, 1 ,8, 1 }1 :" + isBunker(new int[]{3, 7, 1, 8, 1})); System.out.println("{3, 7, 1 ,8, 1, 1 }1 :" + isBunker(new int[]{3, 7, 1, 8, 1, 1})); System.out.println("{3, 7, 0 ,8, 0, 1 }1 :" + isBunker(new int[]{3, 7, 0, 8, 0, 1}));
}
private static int isBunker(int[] i) { int one = 0, result = 0; int prime = 0; for (int j = 0; j < i.length; j++) { if (i[j] == 1) { one = 1; } if (isprime(i[j])) { prime = 1; } if (one == 1 && prime == 1) { return 1; } } return 0; }
private static boolean isprime(int i) { if (i <= 1) { return false; } boolean result = true; for (int j = 2; j <= i / 2; j++) { if (i % j == 0) { return false; } } return result; } }
package bunkerarray;
import java.util.Random;
/*
@author Nabin Adhikari */ public class BunkerArray {
/**
}
static int isBunker(int[] a) { int prime = 0, one = 0, result = 0; for (int i = 0; i < a.length && result == 0; i++) { if (a[i] == 1) { one = 1; } else if (isPrime(a[i]) == 1) { prime = 1; } } if (prime == 1 && one == 1) { result = 1; } return result; }
static int isPrime(int n) { if (n < 2) { return 0; } int countFactor = 0, result = 1; for (int i = 1; i < n && result == 1; i++) { if (n % i == 0) { countFactor++; } if (countFactor > 1) { result = 0; } } return result; }
}
CoreTask#2: BunkerArray - A Bunker array is an array that contains the value 1 if and only if it contains a prime number. The array {7, 6, 10, 1} is a Bunker array because it contains a prime number (7) and also contains a 1. The array {7, 6, 10} is not a Bunker array because it contains a prime number (7) but does not contain a 1. The array {6, 10, 1} is not a Bunker array because it contains a 1 but does not contain a prime number. It is okay if a Bunker array contains more than one value 1 and more than one prime, so the array {3, 7, 1, 8, 1} is a Bunker array (3 and 7 are the primes). Write a function named isBunker that returns 1 if its array argument is a Bunker array and returns 0 otherwise. You may assume the existence of a function named isPrime that returns 1 if its argument is a prime and returns 0 otherwise. You do not have to write isPrime, you can just call it. Function signature is int isBunker(int [ ] a)