473,503 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Programming exercises/challenges

BenI'm learning Python by teaching myself, ... I'm working on the
Benproject Euler problems, but I find that they don't really help my
Benprogramming skills; they are more math focused.

I've found quite the opposite to be the case. I've been programming in
Python for quite awhile and I find Project Euler helps me explore both the
math side of the problems (reminding me of all I've forgotten) but also
forces me to exercise various Python programming techniques and data
structures in ways I typically wouldn't in my day-to-day programming
existence. Some of the problems while conceptually simple to solve are
intractable computationally with naive algorithms.

Here are four of the five (I think) basic ways to solve Problem 1 (find the
sum of all numbers below 1000 which are multiples of 3 or 5). If you run it
notice the wide range of runtimes. Which ones degrade badly as N increases?

#!/usr/bin/env python

import time

N = 1000

t = time.time()
print sum([(n % 3 == 0 or n % 5 == 0) and n or 0 for n in xrange(N)]),
print "%.6f" % (time.time() - t)

t = time.time()
print (sum(xrange(3, N, 3))
+ sum(xrange(5, N, 5))
- sum(xrange(15, N, 15))),
print "%.6f" % (time.time() - t)

t = time.time()
print sum([n for n in xrange(N) if n % 3 == 0 or n % 5 == 0]),
print "%.6f" % (time.time() - t)

t = time.time()
print reduce(lambda x,y: x+y,
filter(lambda n: n%3==0 or n%5==0, xrange(N))),
print "%.6f" % (time.time() - t)

t = time.time()
print sum(set(xrange(3, N, 3)) | set(xrange(5, N, 5))),
print "%.6f" % (time.time() - t)

--
Skip Montanaro - sk**@pobox.com - http://smontanaro.dyndns.org/
Nov 19 '08 #1
0 2025

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2407
by: Mark | last post by:
Hello. I am new to programming and Python and was wondering if someone could help get me started. I picked Python to start learning to prgram because of some things I have read about it (easy to...
8
5422
by: Wous Mant | last post by:
Hello, I am a student in an university and studying C++.Final exams are coming.I'd like to solve as much exercises as possible.I'd like you to test myself with different kind of exercises.Do you...
5
1415
by: Mr. X | last post by:
Hello, Let me begin by saying that I am a strong advocate of science, math and engineering students learning to program... just a skill that they need to experience even if they go another path....
42
2858
by: Kevin Spencer | last post by:
Is it just me, or am I really observing a trend away from analysis and probem-solving amongst programmers? Let me be more specific: It seems that every day, in greater numbers, people are coming...
18
1553
by: Frankie | last post by:
I have been hired to go to a former client of mine and train their staff programmers on ASP.NET. These guys have only Mainframe, MS Access, SQL Server, and VB6 desktop application development...
9
1801
by: Ajinkya | last post by:
Me along with some of my friends have formed a programming group on google to discuss programming problem approaches.... Do join it will be helpful to all beginners and experts same. Here is...
5
1881
by: Martin | last post by:
I am trying to improve my Python skills through some exercises. Currently I am working on Larry's "15 exercises to know a programming language " (http://www.knowing.net/...
13
3098
by: btkuhn | last post by:
Hi guys, I'm learning Python by teaching myself, and after going through several tutorials I feel like I've learned the basics. Since I'm not taking a class or anything, I've been doing...
0
643
by: Stef Mientki | last post by:
hi Ben, btkuhn@email.unc.edu wrote: I'm working on an open source alternative for MatLab / LabView (math again ;-) and there's still a lot to do - (re-)design of the core engine (multi...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.