Archive for Code

TJU Problem 2123 Solution – Java

Head or Tail is a simple counting problem… count 0s or 1s and output the final counts… code’s below.

/*
 * Author: Gal Appelbaum
 * www.galappelbaum.com
 */
import java.util.*;

public class p2123 {

    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int games = scn.nextInt();
        int currentGame;
        while(games != 0){
            int Mary = 0;
            int John = 0;
            while(games != 0 ){
                currentGame = scn.nextInt();
                if(currentGame == 0)
                    Mary++;
                else
                    John++;
                games--;
            }
            System.out.printf("Mary won %d times and John won %d timesn", Mary, John);
            games = scn.nextInt();
        }
    }
}

									

TJU Problem 2001 Solution – Java

Counting Sheep is a very straight forward problem, basic string matching. My solution’s below.

/*
 * Author: Gal Appelbaum
 * www.galappelbaum.com
 */
import java.util.Scanner;

public class p2001 {

    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int cases = scn.nextInt();
        int testCase = 0;
        while(cases !=0){
            int wordCount = scn.nextInt();
            int total = 0;
                String word;
                while(wordCount != 0){
                    word = scn.next();
                if (word.equals("sheep"))
                    total += 1;
                wordCount--;
            }
                System.out.printf("Case %d: This list contains %d sheep.n", ++testCase, total);
            if(cases != 1)
                System.out.println();
            cases--;
        }
    }
}

									

Project Euler Problem 6 Solution – Sum Square Difference

Sum Square Difference also known as problem 6 is very straight forward math… (A) calculate the summation of the numbers squared, and (B)then the square of the summation… then subtract A from B… my solution for this problem is below in C++

#include <iostream>
#include <string>
#include <sstream>

using namespace std;


int main(){
    int sumfirsthundred = 0;
    int squarefirsthundred = 0;
    int sumfirsthundredsqr = 0;
    for(int i = 1; i < 101; i++){
        sumfirsthundred = sumfirsthundred + i;
        squarefirsthundred = squarefirsthundred + (i*i);
    }
    sumfirsthundredsqr = sumfirsthundred * sumfirsthundred;
    cout << sumfirsthundredsqr - squarefirsthundred << "n";
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
    return 0;
}
									

 

Project Euler Problem 7 Solution – 10001st Prime

10001st Prime is another prime calculating problem… nothing hard. my solution is below and written in C++.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int isPrime(int number){
int count=0;
for(int a = 1; a<=number ;a++)
{
if(number % a == 0)
count++;
}
if(count == 2)
return number;
return 0;
}

int main(){
int primecount = 0;
int num = 1;
int prime = 0;
bool working = true;
while(primecount != 10001){
prime = isPrime(num);
num++;
if(prime > 0){
primecount++;
}
}

cout << prime <<"n";
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
return 0;
}

 
									

Project Euler Problem 14 Solution – Longest Collatz sequence

Longest Collatz sequence is a very cool problem which I first encountered (requiring different things, but method was the same) when I started learning C++. The problem states that for any set of positive integers you do the following:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

and that eventually, any number will end up at 1. the problem requires us to count how many numbers are in the chain between n and 1… the solution to this classic project Euler problem is below in Java.

 

/*
 * Author: Gal Appelbaum
 * www.galappelbaum.com
 */

public class p14 {

    public static void main(String[] args){
        int longestChain = 0;
        int finalNumber = 0;
        int count = 1;
        double currentNumber = 0;
        for(int i = 2; i < 1000000; i++){
            currentNumber = i;
            while(currentNumber != 1){
                if(currentNumber % 2 == 0)
                    currentNumber = currentNumber/2;
                else
                    currentNumber = 3*currentNumber + 1;
                count++;
            }
            if(count > longestChain){
                longestChain = count;
                finalNumber = i;
            }
            count = 0;
            System.out.println(i);
        }
        System.out.println(finalNumber);
    }
}