Krunal
BAN USERJust love to code and find the most efficient solution to problems
This solution work only if there are no duplicates:
public class BullsnCows {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String A,B;
A = "forum";
B = "four";
int cow = 0;
int bull = 0;
//int minLength = Math.min(A.length(),B.length());
for(int i=0;i<A.length();i++){
char a = A.charAt(i);
for(int j=0;j<B.length();j++){
if(a == B.charAt(j)){
if(i!=j){
cow++;
}
else{
bull++;
}
}
}
}
System.out.println("The number of cows are: "+cow+" and bulls: "+bull);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package goldbachs.conjecture;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Krunal
*/
public class GoldbachsConjecture {
static final int goldbachNumber = 100;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean isEven = evenNumber(goldbachNumber);
if (isEven && goldbachNumber>2) {
List<Integer> listOfPrimeNumbers = new ArrayList();
listOfPrimeNumbers =findPrimeNumbers(goldbachNumber);
int sizeOfList = listOfPrimeNumbers.size();
done:
for (int i = 0; i < ( sizeOfList - 1); i++) {
for (int j = i+1; j < (sizeOfList ); j++) {
if((listOfPrimeNumbers.get(i) + listOfPrimeNumbers.get(j)) == goldbachNumber){
System.out.println("The pairs of prime numbers for goldbach number are:"+ listOfPrimeNumbers.get(i) +" and "+ listOfPrimeNumbers.get(j) );
}
}
}
for (int i = 0; i < ( sizeOfList - 1); i++) {
System.out.println("The prime numbers for goldbach number are:"+ listOfPrimeNumbers.get(i));
}
} else {
notAGoldbachNumber();
}
}
private static boolean evenNumber(int goldbachNumber) {
if (goldbachNumber % 2 == 1) {
return false;
}
return true;
}
private static void notAGoldbachNumber() {
System.out.println("It's not a Goldbach Number");
}
private static List<Integer> findPrimeNumbers(int goldbachNumber) {
List<Integer> listOfPrimeNumbers = new ArrayList();
int i = 2;
while (i < goldbachNumber) {
if (isPrime(i)) {
listOfPrimeNumbers.add(i);
}
i = i + 1;
}
return listOfPrimeNumbers;
}
private static boolean isPrime(int num) {
if(num == 3 ){
return true;
}
if (num % 2 == 0) {
return false;
}
for (int i = 3; i * i < num; i = i + 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
Still working on the requirement where player cannot jump above another opponent.I'll update that soon.. Till then here is code if some one wants to try.
- Krunal December 20, 2014