1.1.2 Euler Problems && Intro to IntelliJ - Brickwolves/CodeCampWiki GitHub Wiki
Welcome to Code Camp! We're going to begin by working on some warm-up problems that introduce some useful tools in programming.
What's an IDE?
An IDE is an Integrated Development Environment. In other words, it is an application to help us write code. Here's what one IDE might look like:
There are many IDEs out there. Visual Studio Code for example is an application that supports hundreds of programming languages like Python, Java, Javascript, etc. IntelliJ IDEA is an IDE specialized for writing and running standalone Java programs - programs that will run independently on your computer. Meanwhile, Android Studio is an IDE specialized for writing and uploading Java programs to Android devices - most people use Android Studio to develop Android phone applications. We need Android Studio in order to upload code to the robot's Control Hub - a.k.a. the brain of the robot. (FYI Android Studio is based on IntelliJ so the layout of the applications looks almost identical.)
Using Intellij IDEA:
We'll be using the Intellij IDEA for Lab 0.01 because we want to write and run Java programs on your computers. This is especially perfect for these Project Euler problems where we want to do some mathematical calculations.
When you open IntelliJ select New Project. You will have several options, fill them out as follows:
- Name: Euler
- Location: ~/IdeaProjects
- Language: Java
- Build-System: IntelliJ
- JDK: Oracle 18.0.1 (I downloaded the JDK from here)
- Click Create
You'll open up to a page like this:
On the left side you'll see a vertical panel that will allow you to view your project files. Right click on the src folder and you'll see
a list of options. Hover over New, and select Java Class. Call the class Main and hit enter to create it.
You'll see the right panel populate with a new Java file. Every Main class needs a main method! As a reminder, the main method has no return type (void) and takes an array of Strings called args. (Ignore the static part for now) Let's print out a simple message to make sure it works.
public class Main {
// I am a comment
// Below me is the main method that has no return type
// and takes an array of Strings
public static void main(String args[]){
// This is a print statement that prints "Hello world!" to the console
System.out.println("Hello world!");
}
}
You'll notice we still don't have a way to run the code though. The IDE needs to know where the main method is of the Main class in order to run it. We need to add a configuration.
- Click Add Configuration in the top right.
- Click the + sign in the top left
- Add an application
- Name it Main
- You'll notice under the Build and Run section that there's a way to select your Java SDK version (which is just the version of Java you'll be using to compile and run the code you wrote). Additionally, it'll ask you to specify where your Main class is. You can just type Main in there for now.
- Click Apply and OK in the bottom right.
Now that you've added a configuration to IntelliJ you're ready to run the code. There should be a green arrow in the top right of your IDE that will compile and run your code.
Project Euler Problem #1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Project Euler Problem #2
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
This code can also be diagrammed, which is useful for visualizing code:
Mod Problem
Create a function that computes the modulo of two Integers. The modulus operator, sometimes also called the remainder operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. Just like how the plus sign (+) represents addition, the percent sign (%) represents mod. Examples computations:
3 % 2 = 1 because 2 can divide 3 once, and has 1 as the remainder
5 % 1 = 0 because 1 divides 5 evenly
10 % 10 = 0 because 10 divides 10 evenly
3 % 100 = 3 because 100 does not divide 3 at all, so 3 is the remainder.
Using a loop, replicate the mod function. The mod() function will return an integer, and takes in two ints called a and b as parameters. If you need help with the syntax, here's a start...
public int mod(int a, int b){
// Put your code here!
return 1;
}
Project Euler Problem #3
A Pythagorean Triplet is a set of 3 numbers, $a < b < c$ for which $a^2 + b^2 = c^2$
For example: $3^2 + 4^2 = 5^2$
There exists exactly one Pythagorean Triplet for which $a + b + c = 1000$. Find the product of $abc$.
(Bonus) Project Euler Problem #4:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
P.S. If your computer is taking a long time, add some print statements to check out what's happening. What if I told you there was a way to cut the runtime in half?
P.P.S. Why is this critically important to encryption in bank systems?? Find out HERE