My Weblog

Blog about programming and math

Solving problems in C/C++ Vs Pyhton

I used to solve problems of project euler(http://projecteuler.net). This site has quite interesting and challenging problems of number theory. If you want to solve any problem you must have knowledge of atleast one programming language  and  bit of math is also required[:)].My programming language is C and i admire this language but this site does not pose any constraint for programming language because you have to submit your answer rather than code.Now a days i started learning Python and i solved cople of problems in Python.The major difference which i noticed if you are trying to solve the problem in C/C++ , you have to consider overflow otherwise your program will not behave as you want:(  and you have to come up with some sophisticated techniques and algorithm to reach the answer.But if you are using some scripting language like perl , python ,Tcl or Ruby you even need not to bother until unless problem is challenging.Depending on algorithm used execution of C/C++ programs will be enough  less than Python or other scripting language.What is personally fill that if you want to become a good programmer you must learn first C/C++ but its totally my personal opinion.Here is python code for problem 119

#!/bin/python

def fun(n,k):
w=1;
for i in range(k):
w=w*n;
sum=0;v=w;
while w:
sum+=w%10;
w=w/10;
if(v>9):
return sum;
else:
return 0;

l=[];
for i in range(2,100):
for j in range(1,100):
v=fun(i,j);
if ((v==i)) :
#print i,’ ‘,j,’ ‘,i**j;
l.append(i**j);

l.sort();
print l[29];

Advertisements

March 2, 2008 - Posted by | Uncategorized

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: