Wednesday, July 18, 2012

Project Euler Problem 12

Owh. This time, it is about triangle number. The question gave good enough background information, and little good wikipedia show this formula: n*(n+1)/2. If you already read it, you can see a brute strength way of solving this. But, project euler was made to be solved within 1 minute. We may calculate the number of divisors for each triangle number. In essence, two parts are needed. Function to make triangle number from the formula and calculating the number of divisors. We may calculate it by dividing the triangle number to 1 until itself, while counting everytime it's modulus are zero, which in programming term, going for it divided perfectly without fractional part.

It may become very big number. Thus, calculation can be in an hour....

Shortcut. For each factor/divisor, there is another that paired with it. So, we only needed to calculate until square root of that number. But, instead of counting iteration of 1, it will be iteration of 2.

Although, there are some forum posts and websites said something about square number won't work with this short-cut. I create the function suitable for it. But... I tried without it anyway, and answers appeared few seconds later. D:

Uh, I think maybe the square number function are kinda not accurate anyway. But, what's with the square number condition? Oh well.

Tuesday, July 17, 2012

Project Euler Problem 11 (continued)

Finally got it. I forgot the diagonal calculation is for both left-to-right and right-to-left. So, create my unique(?) 'for' loop again, and congratulation to myself. Thank you. :D

Sunday, July 15, 2012

Project Euler Problem 11

Wrong answer. Ah, I was pretty confident... but I'll try again in few hours or so.

Wednesday, July 11, 2012

Project Euler Problem 10 (continued)

hahaha... I cheated in Project Euler. But as well I feel guilty, I tried to understand and built my own program. Even if the structure and solution is the same, at least I tried to understand how it solve very quickly.

First, let's review what is a prime number. Prime number, is a number that can only divide itself and 1. Exception would be 1, it wasn't considered prime number. 2 also is considered prime, and thus the only even number of prime number. Therefore, we can conclude almost all even number are not prime except 2, and all prime number except 2 are odd number, but not all odd number are prime number. We can brute strength programming, but as I had experienced, will take so much time, and may be wrong. Remember, there might be overflow, check your data type.

At larger scale or higher level, there is this sieve thing. Um, Sieve of Eratosthenes. I don't really understand it. But with some reading, it has something to do with square root of the number.

So, from the available solution in http://kahthong.com/2011/12/project-euler-problem-10 , I guess it works like this.
  1. It set the sum to 2, so the first prime number is already being included. in the link, the program use i=2. but it redundant, so I later change to 3. No use repeating the useless process.
  2. i%2 check for even number, and continue the loop instantly. This to eliminate 2 and its even 'siblings'.
  3. Then it start for 3 at the while() loop. This actually signal this program are similar to my prime function, but consider only the odd number, thus the reason for d+=2 iteration.
  4. Then there's the square root thing. I think, this actually a theory that simplify the calculation. The calculation or conditions checking will be reduced for some reason, and still within the range. This might be the real reason it almost just 1 second program speed. I am guessing trying to store every number until 2 million are not efficient way even if that's the theory. Creativity is not my strong point.
  5. it also loop until it reach the square root of the number. which is similar to the original prime function.
  6. last condition check for the primality after the, um... simplification above. if it match, add into the sum
  7. Then display it.
That's it, how I understand it.



*************************************************

Sunday, July 8, 2012

Project Euler Problem 10


I realised something while waiting for my program to finish counting. I have went downstair and watched an episode of some TV program on TV. Then I got back upstair, just to see it is not finished yet.

=| "Owh, still counting..."

=O "What the heaven?"

=| "Still didn't reach 1 million..."

That's about half hour. Thinking a little bit about my program. Hmm.... The computer science book beside me reminded me about software engineering. That focus on how to make more efficient codes and use less memory. So, I guess my program is not efficient.

This is where my imagination power comes in. Simulate and guessing the result in my head, I see that my prime function will calculate too much for large number. So, I cut in between the for loop, breaking it as soon as it is confirmed not a prime (that is, if it can be divided to more than 2 digits-1 and itself-it is false). my codes may be able to become more efficient, but that's just about what I can manage. What do you expect from an inexperience beginner programmer that's too lazy to practice? :P

=0 "OMG so much faster! Awesome!"

1179908154

"It is incorrect."

=x "Imma ouuta here."

Not about programming

You know, I like to see and observe something special. And this just had to catch my attention, screen cap and uploaded here :).






jk how r u?
idk how leh?


Thursday, July 5, 2012

Project Euler Problem 9

Peace. I didn't really do this nicely. I can do math, but my lack of attention span and focus today... I kept doing wrong math calculation, or, um... what was it called again? Equation.... At first, thought about doing it brute force, which explained the function check1000 I made. Then, it strike me that it would be very tedious, if not impossible to brute force finding out the Pythagorean triplets. Honestly, I saw this solution earlier from a Google search. I don't want to cheat, though I did try similar way of that equation... ah, what was it again? Anyway, finally found the part I wrongly placed my square, then I got the answer. I don't want to take credit for this. This is really, not what I do with my own real effort. =_=;;

Wednesday, July 4, 2012

Project Euler Problem 8

Peace. Yay, solved another today. My head feels a lot clearer to do this. This is like, consecutive winning for several days. xD. What I had in mind was to create a subset of 5 digits which will be multiplied with each others. The greatest product will be stored. But, this required us to do several steps:

  1. Split the string to 5 characters (well, the 5 digits as per the question)
  2. Change to integers
  3. Get the products of those 5 digits
  4. Compare the largest product and current product
  5. Display the largest product at the end
Certain conditions that I can put in as I read around the Internet websites are "If it involves digit 0, skip to next as the product will always be 0" and "Iteration can be reduced by 4 as I already reach the last 5 digits" (this explained the i-4 thingy in my for loop ).

Tuesday, July 3, 2012

Project Euler Problem 7

Peace. Solved another problem in project euler today. I may not do it by myself, rather having some references from internet, but I feel satisfied in solving 7th problem today within an hour or so. I still have long way to go, but the great feeling of achievement even from small thing really matter to me.

Monday, July 2, 2012

Project Euler Problem 3

Peace. Been a really long time since the last time I do any programming. So, I felt like trying to do it. And wow, the great feel of achievement for even just a small problem solved, I miss this feeling. Yeah, solved the 6th problem in Project Euler, I am back, the simple beginner programmer. :)