# April 22nd

HOMEWORK

Challenge Homework

Project Euler

Loops

Methods

Objects/Classes

# Lesson Plan

## Review Homework

To start off this week, we will go over the homework problem assigned last week which was the 2nd project Euler challenge projecteuler.net/problem=2. The solution to the challenge should produce an answer of 4,613,732.

Here is an example program I made which solves the problem:

```int num1 = 1; //The second previous number
int num2 = 1; //The first previous number
int current = 1; //The current fibonacci number
int sum = 0; //Keeps a total of all the even numbers found
while (current <= 4000000) { //Runs loop until the most recent fibonacci number is larger than 4mill
if (current % 2 == 0) { //Checks if the current number is even
sum += current; //Adds the current number to the total(if it's even)
}
current = num1 + num2; //Sets current number to the sum of previous 2
num2 = num1; //Sets second previous number to what last previous was
num1 = current; //Sets last previous number to what current number is
}
System.out.println("sum: " + sum); //Prints sum of numbers to the console```

## Go Over Problem as Class

https://projecteuler.net/problem=5
Here is a solution to the problem we did on the board in class:

```int currentNumber = 20;
boolean numberFound = false;
while (!numberFound) {
boolean ourNumberFound = true;
for (int i = 1; i <= 20; i++) {
if (currentNumber % i != 0) {
ourNumberFound = false;
}
}
if (ourNumberFound) {
numberFound = true;
} else {
currentNumber++;
}
}
System.out.println(currentNumber);
```

## Review on Methods

Methods (similar to functions in Arduino) are small chunks of code that can be run using a single line of code elsewhere in your program. Essentially, they are little programs that other programs can use.

Besides just executing chunks of code, methods can also take variables/objects as inputs and a method can output (return) a single variable/object if it wants. The objects a method wants and the objects a method can return are declared in the first line of the method. Here is an example of a method that takes an int object and returns a boolean object with the boolean object being assigned a value of true if the integer is positive.

```public static boolean isNumberPositive(int myIntegerNumber) {
boolean numberPositivity = false;
if (myIntegerNumber > 0) {
numberPositivity = true;
}
return numberPositivity;
}
```

A good explanation of how methods work can be found here: Methods

Once we have a firm grasp of methods, we can add a method to our solution to the project Euler problem (#5) above. The method will take in an integer value and return a boolean value of true if the number is divisible by all integers from 1 to 20.

## Individually Work on Problem

https://projecteuler.net/problem=9

## Homework

The homework due next Saturday is the 10th project Euler challenge. Your job is to write a program that performs what the challenge asks. Feel free to use the resources above to help you or ask me (Kevin) for help by messaging me in Slack, Facebook, email, etc.

projecteuler.net/problem=10

For this homework challenge, write a solution to the project Euler problem (#10) that incorporates a method. I suggest having a method that takes in an integer and outputs a boolean value of true if the number is prime. Feel free to use methods in other ways if you see fit

## Challenge Homework

Here is an extra difficult challenge problem to do for homework. Try your best:

https://projecteuler.net/problem=50

Below is a solution I wrote up to the challenge homework. There are definitely more ways to solve this problem (and certainly better ways too). But if you get stuck, feel free to reference this:

```public static int maxValue = 1000000;
public static void main(String[] args)  {
int startingPrimeForLongestList = 2;
int lengthOfLongestList = 0;
for (int i = 2; i < maxValue; i++) {
if (isPrime(i)) {
int lengthFori = lengthOfConsecutivePrimes(i);
if (lengthFori > lengthOfLongestList) {
lengthOfLongestList = lengthFori;
startingPrimeForLongestList = i;
}
}
}

System.out.println("Sum of primes: " + totalOfConsecutivePrimes(startingPrimeForLongestList));
System.out.println("Starting prime: " + startingPrimeForLongestList);
System.out.println("Length of prime list: " + lengthOfLongestList);

}

public static int lengthOfConsecutivePrimes(int startingPrime) {
int total = 0;
int primesInList = 0;
int lengthOfLongestWorkingPrimeList = 0;

for (int i = startingPrime; i <= maxValue && total < maxValue; i++) {
if (isPrime(i)) {
primesInList = primesInList + 1;
total = total + i;
if (total < maxValue && isPrime(total)) {
lengthOfLongestWorkingPrimeList = primesInList;
}
}
}
return lengthOfLongestWorkingPrimeList;
}
public static int totalOfConsecutivePrimes(int startingPrime) {
int subTotal = 0;
int primesInList = 0;
int total = 0;

for (int i = startingPrime; i <= maxValue && subTotal < maxValue; i++) {
if (isPrime(i)) {
primesInList = primesInList + 1;
subTotal = subTotal + i;
if (subTotal < maxValue && isPrime(subTotal)) {
total = subTotal;
}
}
}