daily funny
Today’s daily funny comes to you thanks to eclipse and the android sdk.

Archive for the ‘ programming ’ Category
Today’s daily funny comes to you thanks to eclipse and the android sdk.

So I’ve decided to start up yet another web project to add to my already long list of unfinished items hidden away in my digital repository closet. This time the language is php and I thought we could try out something new. For the project, I’m basically creating a custom CMS to show article-style pages along with a search feature. My experience with Zend Studio was a favorable one so it will be interesting to see how much of it was due to Zend and how much was due to Eclipse (Aptana is also an Eclipse-based IDE). I’ve also heard netBeans is a good alternative php IDE but I haven’t tried it yet. I’ll be posting again soon with my impressions.
Check Aptana out here
As a way to get reacquainted with c++, I’m revisiting all my PE problems. Here’s the code for #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.
#include <iostream>
int main(int argc, char* argv[])
{
int iMax = 1000;
int iSum = 0;
int iMultiplesOf[2] = { 3, 5 };
for each (int iNum in iMultiplesOf)
{
for (int i = 0; i * iNum < iMax; i++)
{
iSum += i * iNum;
}
}
std::cout << iSum << std::endl;
system("PAUSE");
return 0;
}
Project Euler has quickly become my hobby for when I have small moments of free time. Here is problem #7:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?
and this is my brute force solution:
Public Sub GetPrime() Dim iPrimeMax As Integer = 10001 Dim iPrimeCounter As Integer = 0 Dim iCurrentNum As Integer = 2 While (iPrimeCounter < iPrimeMax) Dim bPrime As Boolean = True Dim iCounter As Integer = 1 While (bPrime = True And iCounter < iCurrentNum) If (iCurrentNum Mod iCounter = 0 And iCounter <> 1) Then bPrime = False End If iCounter += 1 End While If (bPrime = True) Then iPrimeCounter += 1 End If iCurrentNum += 1 End While Response.Write((iCurrentNum - 1).ToString()) End Sub
The problem:
The sum of the squares of the first ten natural numbers is,
12 + 22 + … + 102 = 385The square of the sum of the first ten natural numbers is,
(1 + 2 + … + 10)2 = 552 = 3025Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025
385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
My solution:
Public Sub Difference() Dim iStart, iEnd, iSumOfSquares, iSquareOfSum As Integer iStart = 1 iEnd = 100 iSumOfSquares = 0 iSquareOfSum = 0 For iCounter As Integer = iStart To iEnd iSumOfSquares = iSumOfSquares + iCounter ^ 2 iSquareOfSum = iSquareOfSum + iCounter Next Response.Write(iSquareOfSum ^ 2 - iSumOfSquares) End Sub
Project Euler problem #4:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91
99.
Find the largest palindrome made from the product of two 3-digit numbers.
My solution was a quick brute force attack done while I was eating lunch at work.
Public Sub Palindrome()
Dim iMax As Integer = 999
Dim iX, iY, iProduct, iStart, iEnd, iLargest As Integer
iLargest = 0
Dim bFound As Boolean = False
For iX = 100 To iMax
For iY = 100 To iMax
iProduct = iX * iY
Dim sProduct As String = iProduct.ToString()
iStart = 0
iEnd = sProduct.Length - 1
Dim bStop As Boolean = False
While (iStart + 1 <= iEnd And bStop = False)
If (sProduct(iStart) <> sProduct(iEnd)) Then
bStop = True
End If
iStart = iStart + 1
iEnd = iEnd - 1
End While
If (bStop = False) Then
If (iProduct > iLargest) Then
iLargest = iProduct
End If
End If
Next
Next
Response.Write(iLargest.ToString())
End Sub
projecteuler.net is one of my new favorite addictions. i’ll be periodically posting my solutions to the problems on this site to keep track of my algorithms. this is 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, …
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
and this is my brute force solution done in asp.net:
protected void Page_Load(object sender, EventArgs e)
{
long prevFib = 0;
long currFib = 1;
long sum = 0;
while (currFib < 4000000)
{
if (currFib % 2 == 0)
{
sum += currFib;
}
currFib += prevFib;
prevFib = currFib - prevFib;
}
Response.Write(sum.ToString());
}
this is problem #3:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
and this is my solution which started off brute force but reduces the target number every time it finds a new prime factor causing a drastic reduction in processing time:
protected void Page_Load(object sender, EventArgs e)
{
long target = 600851475143;
for (long counter = 2; counter < target; counter++)
{
// is counter a factor?
if (target % counter == 0)
{
// find out if counter is prime
bool print = true;
for (long i = 2; i < counter; i++)
{
// if counter isn't prime,
// cut out of for-loop and don't print
if (counter % i == 0)
{
i = counter;
print = false;
}
}
if (print)
{
Response.Write(counter.ToString() + "<br/>");
// counter is a prime factor. let's divide target
// by counter to get a smaller target to work with.
target = target / counter;
}
}
}
Response.Write(target.ToString());
}
So I’m doing this XML to wordML conversion using XSLT and there is this annoying blank page at the end of my document. My code looks like this:
<w:body>
<wx:sect>
<w:p>
<w:pPr>
<w:sectPr>
<w:type w:val="continuous"/>
</w:sectPr>
</w:pPr>
<w:r>
<w:t>section 1</w:t>
</w:r>
</w:p>
</wx:sect>
<wx:sect>
<w:p>
<w:pPr>
<w:sectPr>
<w:type w:val="continuous"/>
</w:sectPr>
</w:pPr>
<w:r>
<w:t>section 2</w:t>
</w:r>
</w:p>
</wx:sect>
</w:body>
However, all you have to do to avoid the blank page is move the section formatting information from inside the last paragraph of the last section to a sibling node next to the last paragraph node.
<w:body>
<wx:sect>
<w:p>
<w:pPr>
<w:sectPr>
<w:type w:val="continuous"/>
</w:sectPr>
</w:pPr>
<w:r>
<w:t>section 1</w:t>
</w:r>
</w:p>
</wx:sect>
<wx:sect>
<w:p>
<w:pPr>
</w:pPr>
<w:r>
<w:t>section 2</w:t>
</w:r>
</w:p>
<w:sectPr>
<w:type w:val="continuous"/>
</w:sectPr>
</wx:sect>
</w:body>
Tada! Blank page gone and your document still has excellent structure.