473,378 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 2020

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

Similar topics

4
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.