Wednesday, June 16, 2010

Project Euler Problem 5 in C

    I'm surprised that everyone doesn't get this. It's very simple, doesn't take a lot of time to code and shouldn't take too long to run. Here's the problem:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?


    So nice and easy. Here's my solution in C. It only took 20 lines :D

#include <stdio.h>

int main()
{

int i=1;

while(1)
{
if(i%11==0 && i%12==0 && i%13==0
&& i%14==0 && i%15==0 && i%16==0
&& i%17==0 && i%18==0 && i%19==0
&& i%20==0)
{
printf("%d\n", i);
return 0;
}

i++;
}

return 0;
}


Execution Time: 1.859 s

    The execution time is horribly slow so if anyone wants to comment on how to make it quicker, be my guest!
       NotMyFault

More Hack A Day Madness!

    This is one of the coolest, if not the coolest things I've ever seen. Just watch the video:

    If you're still reading this, or you can't watch the video for some odd reason, what it is is this. A guy pours a tub full of mini soccer balls and basketballs into this tube and they start on their wonderful journey through the most amazing thing ever built out of lego. At one stage, they even get sorted into soccer balls or basketballs! But then they're all thrown back into the same big heap again :D Basically, this is amazing! Even better than the Lego printer!
    So yet again, Hack A Day amaze me!
       NotMyFault

Project Euler Problem 4 in C

    So I've finished another one! This time it's problem number 4. Here's the problem for those of you un-familiar with Project Euler:

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.


    Not too hard. I tried to this a bit different and more optimized than when I did this problem in C++ so here's what I came out with:


#include <stdio.h>
#include <string.h>

int isPalindrome(const int *pTest)
{
char string[6];
int length=0;

sprintf(string, "%d", (*pTest) );
length=strlen(string);

switch(length)
{
case 5:
if(string[0]==string[4] && string[1]==string[3])
return 1;

case 6:
if(string[0]==string[5]&&string[1]==string[4]&&string[2]==string[3])
return 1;

default:
return 0;
}; //End of switch statment

return 0;
}

int main()
{
int test=0;
int *pTest=&test;
int highest=0;
int *pHighest=&highest;
int i=0;
int j=0;

for(i=1000; i>100; i--)
{
for(j=1000; j>100; j--)
{
test=i*j;

if(isPalindrome(pTest) && (*pTest)>(*pHighest) )
{
highest=i*j;
}
}
}

printf("%d\n", highest);
return 0;
}


Execution Time: 0.485 s



    So what I did was had 2 nested for loops and sent the product of the two ints (i and j) to the function isPalindrome() Then I used sprinf() to change the int to a string which I found the length of and checked if it was a palindrome or not.
    So there you have it, Project Euler problem 4.
       NotMyFault

Monday, June 14, 2010

Project Euler Problem 3 in C

    I hated this problem. It was so awkward. I'll explain after I post the description:


The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?


    I know what you're thinking. Not hard at all... That's what I thought too but I had a bit of a problem. I had the code written up for the test input (13195) in about 20 minutes but when I substituted the actual number (600851475143) my compiler complained about it being too large! This caused some serious problems. I was lost for days trying to find out what to do. I used a thing called BigInteger library when I did this problem in C++ but that library isn't available for C. I was asking people on IRC channels and I got some good help of this one guy. He told me that my compiler (Code::Blocks) was a C89 compiler and a number that long needed a C99 compiler so it could store it in a long long int.

&ngsp   Talk about frustrating! I tried to install gcc but I couldn't get it to download! The icing on the cake was that if I had a 64-bit machine, I would have had it but seeing as I don't, I had to find an alternative. That alternative is This. An online C compiler that lets you download the .exe file! It suited my needs perfectly as it compiled it no bother and I got my code finished. I can't give an execution time for this problem but it was fast ;P So here's the code:


#include <stdio.h>
#include <math.h>

#define NUMBER 600851475143

int isPrime(int test)
{
int i=0;
int calculateTo=( (int) sqrt(test) );

for(i = 3; i <= calculateTo; i+=2)
{
if(test % i == 0)
{
return 0;
}
}

return 1;
}

int main()
{
int highest=0;
int i=0;


while(1)
{
for(i=3; ; i+=2)
{
if(isPrime(i))
{
if(NUMBER%i==0)
highest=i;
if(i>=(sqrt(NUMBER)))
goto END;
}
}
}

END:
printf("%d", highest);
return 0;
}



    I guess that's just about it!
       NotMyFault

Friday, June 11, 2010

Project Euler Problem 7 in C

    ProjectEuler once more! In my opinion, it's logical to do this problem before Problem 3 as Problem 3 requires prime numbers too. Also, Problem 3 has the issue of it being an awkward problem due to the high number... Anyways, here's the Problem:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13.

What is the 10001^(st) prime number?


    And here's my code:


#include <stdio.h>
#include <math.h>

int isPrime(int test)
{
int calculateTo = (int) sqrt(test);

for(int i=3; i<=calculateTo; i+=2)
{
if(test%i==0)
return 0;
}

return 1;
}

int main()
{
int howHigh=10001;
int counter=1;

while (1)
{
for(int i=3; ; i+=2)
{
if( isPrime(i) )
counter++;

if(counter==howHigh)
{
printf("%d\n", i);
return 0;
}
}

}

return 1;
}


Execution Time: 0.187 s

    Not too shabby in my opinion! So that's just about it.
       NotMyFault

Wednesday, June 9, 2010

Printer Made Out Of Lego

    Hackaday is awesome. It gets updated every day with new hacks. The hacks are mostly hardware but there's some software hacks too. I spotted this one today and it blew my mind.
    A printer made out of lego. You'd think it's impossible but here it is! There's even little lego men on it! For those of you not going to follow the link it's a guy on his Mac with a page that has "Hello World" in a big heading and then an image of a horse underneath. He then selects the lego printer and the page starts to print. He then zooms in on the printer which consists of a moving felt-tip pen marking the page. As the page moves through, the pen finishes the writing and moves onto marking out the horse. It produces a readable copy of the page! Even the horse is recognisable!
    Anyways, here's the link for anyone interested. You can find the hackaday link in my sidebar. One of my favourite site to waste a half hour!
    That's about all!
       NotMyFault

Project Euler Problem 2 in C

    So ProjectEuler once again. This time it's problem two. Problem is:
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.

    So it involves going up to 4 million... that was a bit confusing. Originally, I had an array of ints (int fib[4000000]) but that didn't last too long as every time I ran it it would crash. To get around that, I lowered the figure down until it was still reaching all the fibonacci series below 4,000,000 but wasn't going to crash the program. So here's the code:

#include <stdio.h>

int main()
{
long int fib[400000]={400000, 0};
long int total=2; //fib[1] is not added as the loop starts at fib[2]
int i=0;

fib[0]=1;
fib[1]=2;

for(int i=2; i<=400000; i++)
{
fib[i]=fib[i-2]+fib[i-1];
if(fib[i]>4000000)
goto end;
if(fib[i]%2==0)
total+=fib[i];
}

end:
printf("%d\n", total);
return 1;
}

Execution time: 0.171 s

    Yeah, yeah. I know. Don't use goto but it was just so handy! again, all comments welcome.
       NotMyFault


Tuesday, June 8, 2010

Project Euler Problem 1 in C

    I enjoy the Project Euler problems. I've never asked for help even though I've struggeled a lot at them. Even still I've only done ~10 in my preferred language, C++. I'm now learning a little C and I've decided that Project Euler would be a great place to practise what I've learned. So here's my solution to problem 1 in C:



#include <stdio.h>

int main(void)
{
int total=0;
int i=0;

while(i<1000)
{
if(i%3==0 || i%5==0)
total+=i;
i++;
}

printf("%d", total);
}

Execution time: 0.042 s


    That's all then. I'll post up my solutions to the other problems as I finish them. All comments welcome.
       NotMyFault

My Phone

    I have a mobile-phone. It's probarbly the cheapest, worst-looking, basic phone you can buy but I love it. Wanna know why? Because I can do practically anything to it! I can kick it off stairs, throw it in a bin, drop it on granite, throw it onto concrete, from a second floor I can throw it into a wall, etc, etc.
    Don't believe me? Watch the videos! Also, if you have anything you want me to do with my phone just comment and I'll see what I can do!



Kicked Down Stairs





Thrown Out of Window





Thrown Onto Granite





Thrown 12ft Into a Bin





Thrown Down 2 Stories At A Wall





    These videos are 100% real by the way!
       NotMyFault


Old Computer

    So there's been this computer lying around my house for the last couple of years. It has Windows 98 on it and doesn't start. I decided I'll do something about it. That was a couple of weeks ago...

    So I decided to take it apart and take what I could but seeing as this was my first time doing something like this, I figured that this old waste of space was perfect to butcher! I successfully managed to take everything out of it and have decided to find out what everything is and try to put it back together again! This won't end well...

    Anywhoo, here's some pictures to show anyone who doesn't know what exactly is inside a computer!








       NotMyFault


NotMyFault

    Hey, I'm NotMyFault and this is my blog about hacking, programming and whatever else takes my fancy :D
    I suppose I should make something useful out of this site but really it's just here to try make a little bit of money for me on the side with the AdSense ads (Sorry if they end up being big images) that are on the bottom of every blog post. It's also here as a document of me learning about computers in general so there'll be a huge mix of topics on here.

    That's that then, first post down!
       NotMyFault