Thursday, May 2, 2019

Programming Challenge no. 2: Fibonacci Sequence (Understanding Pseudocodes)

We are finally solving another programming challenge from Project Euler (after a long, long, long time).

 Our challenge for today:
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.
Before we open our compilers to start programming, we need to understand what the problem requires and the conditions we need to consider.

For this challenge, we'll use Java. (Actually, as long as you understand how the code works, you can use any language you feel comfortable to use.)

Analyzing the Problem


What we need to get: 
sum of the even-valued terms in the Fibonacci sequence.

Conditions:
1.) The numbers we need to add are in Fibonacci sequence.
2.) The numbers we need are less than four million (That's a big number!)
3.) The numbers we need are even.

Now that we have established our objective and requirements, let's create a pseudocode for this challenge.

Creating the Pseudocode


A psuedo-wha?

I've mentioned the meaning of this word in the previous puzzle but let me explain it better here. Let's break down the word. When you Google the word "pseudo", the results will tell you that it means "fake" or "mock". So when you say someone is a pseudoscientist, that person is a "fake scientist".

So we're making a "fake code"?

Err.. Well, to be precise, we are writing a code that resembles a programming language. It is not a real code since if you run it in a compiler, it would create errors. This makes sense because pseudocodes are written not for computers but for humans. With pseudocodes, you need not worry about programming language syntax.

Below is an example of pseudocode:

Declare fibonacciFormer, fibonacciLatter, currentFibonacci, limit and sumOfEvenFibonacci to Long
Initialize limit to 4000000
Initialize sumOfEvenFibonacci to 0
Initialize fibonacciFormer to 1
Initialize fibonacciLatter to 1
Initalize currentFibonacci to fibonacciFormer + fibonacciLatter 
While currentFibonacci is lesser than limit
     Check if currentFibonacci is even
         if True
             sumOfEvenFibonacci = sumOfEvenFibonacci + currentFibonacci
      fibonacciFormer = fibonacciLatter
      fibonacciLatter = currentFibonacci
      currentFibonacci = fibonacciFormer + fibonacciLatter

      return sumOfEvenFibonacci


Pause and try to understand what is happening. If your brain is refusing to process the "fake code" above, you'll have a hard time understanding the real one so let me enlighten you. However, if you figured out what the pseudo code does, you can move on to the coding section.

Let's start with what a Fibonacci sequence looks like. The figure below shows the Fibonacci sequence below 100.



So the next number is the number of the two preceding numbers in the sequence. Can you guess what's the next number after 89? Just add 55 and 89!

Now that we understand the Fibonacci sequence, let's go back to our pseudocode. You'll see that we have variables called fibonacciFormer and fibonacciLatter. These contain the preceding numbers while currentFibonacci is the sum of these numbers. Basically, currentFibonacci is the next number in the sequence. We initialized fibonacciFormer and fibonacciLatter to 1 because the Fibonacci sequence always starts with 1. We also initialized sumOfEvenFibonacci to zero.

Now the conditions! We are given the limitation of getting the numbers not greater than 4 million. We can hard code the condition to the number but it is wise to use a variable (in this case, our variable is called limit) so we can easily modify the code in the future when the requirements change.

1.) We will check if the currentFibonacci is lesser than our limit.
      If it is, then we will go inside the loop and arrive in our next condition.
2.) Since the problem set specifically wants even numbers, we need to check if the currentFibonacci is even.
3.) If it is, we add it to our current sumOfEvenFibonacci.
     If it is not, we skip it.
4.) We will then re-assign the values of our fibonacciFormer to get the value of fibonacciLatter.
5.) The variable fibonacciLatter gets the value of our currentFibonacci.
6.) Then we get the next number of our sequence by adding fibonacciFormer and fibonacciLatter and assign it to currentFibonacci.
7.) Then back we go to our loop until finally, we reach the limit.
8.) We exit the loop and we return the sumOfEvenFibonacci, which should contain the sum of our even fibonacci numbers.

And that's what our pseudocode is trying to do.

Coding


So here is our pseudocode written in Java:

public long getSumOfEvenFibonacci(long limit)
    {
      long fibonacciFormer, fibonacciLatter, currentFibonacci, sumOfEvenFibonacci;
      sumOfEvenFibonacci = 0;
      fibonacciFormer = 1;
      fibonacciLatter = 1;
      currentFibonacci = fibonacciFormer + fibonacciLatter;
   
      while(currentFibonacci < limit)
      {
        if(currentFibonacci % 2 == 0)
          sumOfEvenFibonacci = sumOfEvenFibonacci + currentFibonacci;
     
        fibonacciFormer = fibonacciLatter;
        fibonacciLatter = currentFibonacci;
        currentFibonacci = fibonacciFormer + fibonacciLatter;
      }
   
      return sumOfEvenFibonacci;
    }

It looks like our pseudocode, right? Except this one has brackets and semi-colons. Also, notice that to check if the number is even, we used the expression currentFibonacci % 2 == 0. The percentage symbol represents the modulo operation. We'll get to that in another post sometime.

Recommendation


If you run this Java code, it will give you the answer to the second puzzle in Project Euler. But this is not an efficient algorithm.

Algo-what now? Rhythm? We talking about music now?

Let's deal with algorithms in the next puzzle, aight? For now, give yourself a pat for taking the time to understand the Fibonacci sequence and how to get the sum of its even numbers.

Sunday, March 31, 2019

The Past Five Years

I'm not sure if you've noticed but there's a gap of five years between my recent post and the post before it. The only excuse I could give for this gap is: life happened.

I won't go into details what exactly happened. I'm simply thankful that I can blog again.

There are some changes though. I won't be giving ActionScript and Flash tutorials anymore. I know. I know. You might be saying to yourself: "But you never gave any tutorials of sort." Well.. Now I definitely won't :P I loved programming with AS3 and Flash but since most browsers have dropped support for it, there's no point learning it from here on forth. (I might still make games with it though.)

What are the other changes?

I have recently enrolled for postgraduate diploma in Information Technology. I am now currently learning new technologies that I would love to share in this blog:


  • ASP.NET
  • C#
  • Node.js
  • PHP
  • MySQL
  • Continuous Integration/Deployment


and more! I will still continue giving guides for the Project Euler puzzles. I will still share mathematical and computer science concepts. And I will still rant about my programming life.

I hope you will continue to learn with me in this journey!

Poroporoaki! (Oh. Did I mention that I have recently moved to New Zealand? Yes. I'm studying in NZ now.)

Learning Web Development: CSS

CSS or Cascading Style Sheets is used to layout and style websites. HTML or Hypertext Markup Language is used to structure information in websites. You should not confuse the purpose of these two languages. When writing HTML, think how you want your information to be structured, not displayed. To design your website's appearance is CSS' job.

Layouting
One of the very first things that a web designer has in mind when designing a website is its layout. How big is the header? How many columns should the webpage have? Should the navigation stay on top or stay at the side?


This is an example of w web layout with a header, navigation menu, a main content between two contents and a footer at the bottom.
Example of Web Layout (Source: W3School)



This is an example of website layout taken from W3School. Another great website for learning layouting with CSS is the blog called Learn CSS Layout. Learn CSS Layout teaches the basic layouting and briefly introduces flexbox. To learn more about Flexbox, here is a complete guide to it. You might also want to learn about Grid, a grid-based layout system using columns and rows. The CSS Grid is also a great resource in learning Grid layouting.

CSS Coding Guidelines
Working on a project by yourself means you're only communicating with yourself so there's no need to follow conventions. However, most developments of software happen by teams and guidelines are needed to make sure that everyone is on the same page.
Here are some guides to check out:


You might be thinking, "Wait. So which one should I follow?" If you're working by yourself, choose the one you're comfortable with. If your company has different guidelines, follow the one that gets you paid (or make suggestions to improve their guidelines.)
Teams benefit from guidelines but it's also worthwhile to follow them even if it's a one-man project.

HTML/CSS Tools
  • The HTML CSS Javascript website provides free online tools to maximize your coding efficiency in web development. They have cheatsheets, templates, code cleaners and more. 
  • Do you want to create animations using CSS but doesn't know how to? Do you want to simply specify the layout you want for your website and get the CSS styles for it? Check out The Ultimate CSS Generator. (This website is actually called Web Code Tool and is not limited to CSS. You can also produce HTML and Javascript codes.)
  • Have no time to style your menus? Luckily there's a CSS Menu Maker which speeds up your web development by providing various menu themes.
Note: These are simply tools to aide you in your web development. Don't rely on them and ditch learning CSS. Knowing how CSS works and how to read CSS code are necessary to be an effective web developer.

Web Development Learning Websites
Want to enhance your knowledge in CSS or web development in general? Here are some websites to visit to unleash your web development skills:

I hope you found these resources helpful. Pang signing out.

Friday, November 7, 2014

Mathematical Solutions: Multiplying Big Numbers


If you are the type of person who has little interest in numbers and would rather draw doodles in your test papers than solve, I know what your feeling. I wish my passion for puzzles equates my skills in numbers, but my brain tends to fly to a completely different world when I start seeing digits.

Big numbers are the scariest, aren't they? I dread the days when the teachers would hand out exam papers and strictly implement the rule: NO CALCULATORS. How am I supposed to calculate these three-digit numbers in half an hour? All twenty sets of them?!

Today, I found out what I should have found out when I was still in school. If you're still studying and struggling in Math, you're lucky to have stumbled upon this article.

This day is the day when you find magic in Mathematics, all thanks to the creative minds of Japanese people. All you need to do to be amazed is click the play button below.


Nuff said. This is truly an art of Mathematics. I'm not taking Math exams anymore, but I could still use this  when I'm kidnapped by mathematical terrorists and I end up becoming one of them because I am capable of multiplying BIG numbers.

Wednesday, August 29, 2012

Mathematical Solutions: Multiplying by 9

We've always wanted a faster way to solve math problems. That's why we always have our handy dandy calculator. But what if you don't? You wouldn't be always carrying your calculating device everywhere, would you?

If you want to answer math problems in a snap, here's a useful math shortcut that you might wanna try before you become too dependent on calculators.. (To the point that you'd actually type 1 + 1 when the answer is already obvious).


Multiplying a number by 9.

169 X 9 is equal to? Something. You stare at the numbers with annoyance. But seriously, try and calculate it by yourself. You can write it down on a paper or mentally solve it. NO CALCULATORS!

Done? Okay. Here's the trick. You can actually solve this by using the number 10. How?
Watch:

169 X 10 = 1690
1690 - 169 = 1521

Try checking if it has the same answer as 169 x 9. It's the same, isn't it? So how did it happen?

Remember that 10 - 1 is 9.
So, basically: 169 x 9 == 169 x (10 - 1)
And we all know dividing any number to 10 is easy.

Well, it's faster if you like subtraction and subtraction is much easier than multiplication, right?


There'll be more Mathematical Solutions so stay tuned :3

Tuesday, August 14, 2012

A Dilemma of a Programming Student: Midterms Week part 1

Choosing a programming course because you like programming doesn't always mean fun.. Because most of the subjects you'll encounter will not even involve programming..

And so here I am, facing my PC trying to understand stuff that doesn't interest me..

But I guess they'll come handy sometime.. I mean.. I'm sure by solving the series of unfortunate events of my life using Taylor's series, I can be enlightened and be able to appreciate life..

I'm not kidding.. Life, like numbers and other mathematical entities, has a pattern :P

Well, I guess that's it for today. I have to go and become a nerd who studies math stuff D:

P.S.
I'm gonna fail.. I think D:

Saturday, August 11, 2012

Programming Challenge no. 1: Sum of Multiples of 3 and 5

So I stumbled upon this site called Project Euler (It was named after Leonhard Euler who had a huge contribution in discovering infinitesimal calculus and graph theory.. Not sure if I'm thankful about his discovery.. But I guess I should since it helped improve a lot of things..)

What am I doing in Project Euler? Well, you see.. I promised to answer some programming challenges for you and as I googled the words "programming challenges", I saw this site. It's not bad actually.. I kinda like it :3


/* Warning: Must have at least basic programming experience */


So let's start with their very first challenge:
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.
You can go ahead and get your calculator. I'm not stopping you. However, I must warn you that you're gonna waste at least 15 minutes of your life pressing the multiples of 3 and 5.. and with a huge possibility of committing a mistake..

If I were you, I'd find an algorithm that will give me the final result. One way is solving it the programming style :P


For this challenge, I'll use C as my programming language.
I'll use Turbo C as my compiler. (This doesn't matter actually)
And my left brain to solve the problem! ('Cause for some reason, my right brain hates numbers..)


Analyzing the problem


We are asked to compute the sum of the multiples of 3 and 5. 

In their example, the multiples of 3 and 5 below 10 are given: 3, 5, 6 and 9. Adding these multiples will give us 23. We need to do the same thing. Only this time, we need to find the multiples below 1000.

It's tempting to list down all the multiples then add them together.. but that would be an inefficient way to solve this challenge..

What we need to do is analyze and take note what we need to solve the problem:

  1. Only multiples of 3 and 5 are allowed.
  2. Numbers like 15, 30.. etc. are multiples of 3 and at the same time, of 5.
  3. Multiples should be below 1000.
So basically we need to add numbers below 1000 that are either multiples of 3 or 5 or both..


Creating the Pseudo code


Pseudo codes are useful in creating programs. Since they use simple statements instead of a specific programming language's syntax, they're very easy to read.

So here's the pseudo code for our solution:

sum as unsigned long    // We create a variable sum that holds unsigned long integers 
num as integer             // We create a counter num that will go thru 1 to 999
While num is less than 1000     // Set a condition that will prevent the loop from going beyond 1000
          if num is divisible by 3 or 5     // Check if number is a multiple of 3 or 5
             add num to sum                  // If it is, add it to sum
          add 1 to num                          // Iterate num
print sum                                          // Show output on screen

What will happen is, the computer will go through all the numbers from 0 to 999. Each will be checked if it's divisible by 3 or 5. If the number is indeed a multiple of 3 or 5, it'll be added to the sum.

Now that wasn't so hard right? Now, let's go to the exciting part: coding..

Coding


I'll just show you directly what the code is and explain each part.

void main(){
     unsigned long sum;
     int num;
This part contains the declaration statements. Here, we declared two variables: sum and num
sum shall contain the total value of the multiples. It's unsigned long since we expect a large number. num will be iterated from 1 to 999 so it's basically like a counter.

for(sum = 0, num = 0; num < 1000; i++){
      if((num % 3 == 0) || (num % 5 == 0)){
             sum = sum + num;
     }
}
This loop searches thru the numbers below 1000 and finds the multiples of 3. The condition uses || known as the symbol for OR because we need all the multiples of 3 and 5. Using && (known as AND) will give us only the multiples of both 3 and 5.
printf("%lu", sum);

Note:  The sum should be long since we're dealing with a huge number.


Conclusion


So? Did you understand how to solve this programming problem? If you're still having trouble comprehending the method, don't hesitate to comment.

By the way, if you want a shorter code, here's another version of it..

void main(){
     unsigned long sum;
     int num;
     for(sum = 0, num = 0; num < 1000; sum = sum + ((num % 3 == 0 || num % 5 == 0) ?           num : 0), num++);
     printf("%lu", sum);
}
The italic part of the code is using ternary operation. I'll talk about that next time. I don't want to overwhelm you.

You might be asking: So what's the answer? That would be known only if you program the code yourself :P
I don't wanna spoon-feed you with answers to the Project Euler's challenges. My goal here is to relay what I can about programming. Not become a source of direct answers from a site that challenges you to solve programming problems.

If you want your answers checked or you're having troubles with the code, you can email me directly.

I hope you learned something :)

Sunday, August 5, 2012

Programming Challenges: Let's dig 'em!

I have no idea what kind of tutorials I should make.. I mean, everything is in the internet already.. So what I thought of is to find a programming challenge that requires you to program something and stuff. As I try to solve the challenge, I will also explain how the program should work

So starting from today, I'll post programming challenges that might help you analyze things when it comes to programming.

Yosh! Let's hunt for some programming puzzles to solve!

Friday, August 3, 2012

Learning Flash: Creating Point and Click games the Fun Way

/*WARNING: The video presented in this blog is now unavailable. */

Haven't really posted anything related to programming, have I?

I apologize for my lack of dedication to this blog. I would try my best to keep you updated with my journey.. I  mean, with stuff that involves programming.. and computer stuff.

Not posting in this blog doesn't mean I also lack dedication in programming. In fact, it's the contrary. I've been so focused in creating games and learning new programming languages these past few weeks.

Okay. Let's start talking variables and computer logic before I turn this whole blog into a dairy.

I've been searching for great tutorials about Flash. Why? See post Why, I Love My Work Even More. It's a personal sharing of what's up with my life. If you don't care about it, then let's proceed to the fun part!

If you're a total newbie when it comes to Flash, I suggest you google some tutorials you're comfortable with. Sometimes, there are people who understand better when reading direct to the point words. I'm part of the other side who wants to learn things in a weird manner.

But don't worry. If you want serious-talking-but-not-that-boring kind of tutorials, I highly recommend Foundation Flash. Their tutorials are very helpful and they're completely free too! I've established my Point and Click gaming instincts here.

But if you're just like me who wants to learn stuff in the weirdest way possible, then I suggest you listen to Jonbro with teh infront of it. Unfortunately, he's not into making tutorials but this video saved me from going through information-packed-yet-energy-draining tutorials.No offense to the lecturers. I just like entertaining stuff like this one:



Soo.. What do you think? Learned anything or were you just distracted with all the ranting? Well, I was distracted but I remembered all the things I needed to do after. Even though the price is to listen to a 20-minute video which can be compressed into a 10-minute one, I was glad to make the deal.

If you wanna learn more Flash tutorials, I'll get you updated with them. I'd probably post my progress in this blog together with the information I've learned. So stick around.

Why, I Love My Work Even More

Okay. First I must warn you. This a post not related to any programming facts or help. I'm simply sharing my experiences here. So if you don't wanna waste your time stalking my life, it's okay :3 You can go read the important stuff in this blog. Just browse for some tutorials.

So, where was I? Oh, right! Work!

This week has been the busiest yet the most exciting one for this year. To be honest, I'm quite pleased when my boss told me to create Flash games for their site. I'm working for them for a month already and honestly, I love my work.

However, my previous tasks don't involve programming at all. It's fine since my work is something I always do: casual gaming, plus writing and checking grammars. It's fun yet very stressful since I have to play at least 60 games per week.

The other task helped widen my web developing knowledge though. When I was searching for jobs back then, a lot of company were looking for people who know WordPress. I was thinking it would involve a lot of blogging and writing. I mean, that was how I saw WordPress before. A blogging site.

But then I was completely wrong. My boss told me to create a blog specifically for puzzle games. The site is hosted under WordPress. I've used WordPress before but I like Blogger more. So when I started tinkering my site, I was completely overwhelmed with codes and plug-ins. I thought I'd only do drag and drop in making my website. I was oh so wrong.

I had to set-up my site for a week! But it was not a wasted week. I've learned about WordPress and how you edit your own site. My PHP class didn't fail me. I've asked help from my friend, accidentallyc. You see, I like surfing but I don't like creating sites people would like to surf for. I'm into games and database analysis.

So what happened was, I've asked my friend to design my website with stuff and add creative stuff while I tinker the PHP stuff. Fortunately, when this friend of mine started his On-the-Job-training, his involvement with my WordPress adventure helped him. It's a win-win!



So I guess I've finally bore you with the past. Let's discuss the present events now!

I've been casual gaming and commenting games for a month when suddenly a door opened for me. The door to finally use my Flash and ActionScript knowledge. Well, I'm still a noob in ActionScript.

So this door opened when my boss said we need to create games we can distribute. These games will link to our site since the AI in Google can search through Flash already. This would help them rank in the search engine and I don't know why I'm ranting about this. Just know that I'm developing games to help the site gain popularity. I'll leave out the how-does-it-work for later.

Anyway, this made everything better for me. Why? Three reasons:


  • My On-the-Job training is finally related to my course.
  • I get to improve my game developing knowledge.
  • Making games is my ambition so technically, I'm halfway in achieving my dreams. *angel singing*
And that's why I love my work even better!

*deep breathe*

Well, this is a pretty long post.

I'm surprised you read through all that boring stuff. I assume you are simply bored right now.

Friday, July 20, 2012

So.. What's up?

Woah! I just started making this blog and.. I'm starting to get lazy. Ooops..

Well, not really lazy but rather busy.

I've been doing some projects now and sad to say, most of them are not even programming.

In our Android project, I'm one of the designers. So basically, instead of a compiler, I'm using Photoshop. But most of the time, I'm using Fireworks since I'm a better vector artist.

Oh well.. I guess I need to be a programmer by myself. Is it because I'm not too good in Programming that I was assigned in Designing? Hopefully not. But in all honesty, I'm kind of glad I was not the programmer. I'm not really into mobiles.

To start my programming journey, I'll be making an HTML5 game. A simple one. So I guess I need to master my Javascript and JQuery then.

Oh, and I'm gonna post some projects and researches here just in case people need them as much as I needed them. Helping out fellow programmers, that's what we do, aye? Well, except when we're hacking each other.. that would be.. unnecessary already.

I'm gonna try and keep you update with.. uhmm.. Programming. Yes. Of course! This is why we created this blog after all.

Saturday, June 23, 2012

Your program is now running..

The number of errors in programming is immense and the search for better solutions has been a tough journey for all the programmers out there. This site is the beginning of my voyage to become a better programmer and to solve the different defects in my programming. This is a personal site, by the way, and at the same time, an educational one. For every project and experience I encounter, I will post them here so I can remember my adventures in the future and for others to relate to my struggles.

So, welcome and ahoy! The voyage has been started. Let us now sail.

And yes, I may become a pirate or an explorer or a computer chip from time to time.

Sorry.. I'm not really used to long introductions :P