Welcome to hackerLog

Avi Gupta
hackerLog
Published in
3 min readOct 13, 2018

So, a little about this log…

I did this to share the things that I love.

For starters, processing.

Anything can be represented in numbers. so why not share some things about numbers?

Processing is a scripting language based on Java that is good for rapid prototyping and understanding basic computer science, like “how is that video game made?” It’s a simple language that is easy to understand.

Let’s say I want to make a rectangle with its top left corner at (50, 20) with a width of 40 and a height of 30.

I just do this:

rect(50, 20, 40, 30);

When I started using processing, it was kind of hard at first, but eventually, I got used to it.

Other languages include C (I made a square root program there, but then I deleted it accidentally), C++(which sucks, by the way), Python (great for text role-playing games), Swift, and Android (both are used on devices). All are great to learn, some more challenging (not harder) than the others. But none the less, at some point in your life, you are going to use a computer.

So about the numbers, my dad and I made a program on processing that could calculate the square root of a number that the user entered that was at the most 7 digits long.

We used the bisection method:

  1. start with 1/2 of the input number and test if that is the correct square root by squaring it.
  2. If the root² is greater than the desired number, than we make the greater part of the range the root.
  3. Otherwise, if the root² is less than the desired number, then we make the lower part of the range the root.
  4. We keep doing this until the root² = the desired number.

for an explanation of the bisection method, click on the link below:

https://en.wikipedia.org/wiki/Bisection_method

It took us a while to figure out how to get the user input, but we did it.

void keyPressed() {
if( key >= '0' && key <= '9') {//only accepts numerical characters
if(input.length() < 8) {// it should be 7 digits or less
input += key;
print(key);//prints the char
}
else {
println("done! press enter!");//does that if you are trying to get more than 7 digits
}
}
else if(key == '\n'){
num = Integer.parseInt(input);//converts the string of numbers into an integer
input = "";
println(" we'll find sqrt for: " + num);
findSqrt(num);
}
}

then we calculated the square root: (see comments for explanations)

void findSqrt(float num) {// the function that calculates the square root
float lf = 0;// the smaller range of the calculation
float rt = num;//the greater range of the calculation
float prev = 0;
float root = 0;
for(int i = 0; i<1000; i++){// here are the rules for calculating the square root
root = (lf + rt)/2;
// this states that 0 + the number /2 = the root (the calculation)
if((root * root) == num) {
break;
// if the root^2 is the number that you are trying to find the square root of, it breaks off from the process
}
else if((root * root) > num) {
rt = root;
// root^2 > num, then the greater range of the calculation is the root
}
else {
lf = root;
//root^2 < num, then the smaller range of the calculation is the root
}
if(prev == root){
break;
// only works if num == 0
}
else {
prev = root;
}
println("iter["+i+"]("+lf+","+rt+"):" + root);//prints each iter before the break (usually around 30 iters for a non perfect square)
}
println(" value of root["+root+"] squared is: " + (root *root));// prints the true root with the number that you wanted to find the square root of
intro();// goes back to introduction
}

whole code available on github here:

https://gist.github.com/racecraftr/bbdee2f5bab098050976ceb4af565b28

I like doing most of my projects digitally, because it is easier to fix what you messed up. I just want to share all of these cool projects that I will show in this blog. (I might include some gaming, in case my dad allows me! 😉)

Signing out ‘till next time,

Avi Gupta.

--

--

Avi Gupta
hackerLog

Named after the first 2 games I got on my xBox, Forza and Minecraft. Also, i have a blog. Real name is Avi.