Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69508
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.012 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'algorithm': 0.04; 'familiarity': 0.07; '#include': 0.09; 'main()': 0.09; 'cc:addr :python-list': 0.11; 'skip:= 70': 0.12; "wouldn't": 0.14; '(other': 0.16; 'braces': 0.16; 'count,': 0.16; 'easier.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'is;': 0.16; 'notably': 0.16; 'optional': 0.16; 'partly': 0.16; 'readable': 0.16; 'recurse': 0.16; 'subject:unicode': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'cc:addr:python.org': 0.22; 'decide': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'equivalent': 0.26; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'relative': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'that.': 0.31; 'break;': 0.31; 'closer': 0.31; 'comparison': 0.31; 'dropped': 0.31; 'end,': 0.31; 'factor': 0.31; 'second,': 0.31; 'quite': 0.32; 'guess': 0.33; 'not.': 0.33; 'could': 0.34; 'something': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'scheme': 0.36; 'tight': 0.36; 'version:': 0.36; 'doing': 0.36; 'two': 0.37; 'clear': 0.37; 'depends': 0.38; 'pm,': 0.38; 'easy': 0.60; 'numbers': 0.61; 'simply': 0.61; 'simple': 0.61; 'first': 0.61; 'making': 0.63; 'name': 0.63; 'more': 0.64; 'url:c': 0.67; 'acts': 0.74; 'prime': 0.74; '(let': 0.84; 'actually,': 0.84; 'technically': 0.84; 'cast': 0.91; 'to:none': 0.92 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=GuA1zHuqJuaQS9DxzWCnOOPz/ZJtPNIwvOgFYq1DnLs=; b=Q2n+e+KJW46Q4Jq53/Kx255pbWvz9RxhiKo2ndm9Hcm/djFj8JUSxqxVvgJrnbU/J9 1sOAmq+ZT3cz5HfDLODFtXGhR5H53MjIBDlkxAaWpV2TyzeVoNHZ1CoUu2pPYxj/pyki 4UnH4SsqIB4pdj3FFHqFXiU2b6nnMzN8CMNxEpViCWyvVJelzhUYCKW0UWvmKndRWuA2 7HyPzfieZ21Gqmv+R+Fz3FaOFFn1fNn4PGLRMGvTIPGzk/sHNguxe5DEkoLrrBkaTEO1 3C4w0iyziwo7Ue3mYkMfiOJZD7w3oomHbbYWLHsdH97pDyeIYR8g2Vn1Gi49XiafRsLI IPbw== |
| MIME-Version | 1.0 |
| X-Received | by 10.68.235.6 with SMTP id ui6mr30881847pbc.45.1396356841189; Tue, 01 Apr 2014 05:54:01 -0700 (PDT) |
| In-Reply-To | <87ob0ltfze.fsf@elektro.pacujo.net> |
| References | <lgsi07$k1p$1@speranza.aioe.org> <mailman.8531.1395775491.18130.python-list@python.org> <5331D902.3030902@gmail.com> <53321819$0$29994$c3e8da3$5496439d@news.astraweb.com> <lh1g3h$meg$1@speranza.aioe.org> <CALwzidk+4diadosJ0bDFTj-OjU9ib712iPMBVxvqZfxAsY2cJg@mail.gmail.com> <53393BA4.2080305@rece.vub.ac.be> <CALwzidkWc+jQtyN7-nFd4wZCryqLNfNc2okVL_kSbCBkE6nuQQ@mail.gmail.com> <5339C281.7080300@rece.vub.ac.be> <CALwzidkTRYbpeBCY7gxSXa=ZNimaAe69mGbT0d9sbehHfHgvOw@mail.gmail.com> <533A768F.5080102@rece.vub.ac.be> <mailman.8790.1396344274.18130.python-list@python.org> <87txadtmwq.fsf@elektro.pacujo.net> <mailman.8791.1396346325.18130.python-list@python.org> <87ob0ltfze.fsf@elektro.pacujo.net> |
| Date | Tue, 1 Apr 2014 23:54:00 +1100 |
| Subject | Re: unicode as valid naming symbols |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8797.1396356845.18130.python-list@python.org> (permalink) |
| Lines | 130 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1396356845 news.xs4all.nl 2915 [2001:888:2000:d::a6]:58717 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:69508 |
Show key headers only | View raw
On Tue, Apr 1, 2014 at 11:02 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> ========================================================================
> #include<stdio.h>
>
> int main()
> {
> int n, i = 3, count, c;
>
> printf("Enter the number of prime numbers required\n");
> scanf("%d",&n);
>
> if ( n >= 1 )
> {
> printf("First %d prime numbers are :\n",n);
> printf("2\n");
> }
>
> for ( count = 2 ; count <= n ; )
> {
> for ( c = 2 ; c <= i - 1 ; c++ )
> {
> if ( i%c == 0 )
> break;
> }
> if ( c == i )
> {
> printf("%d\n",i);
> count++;
> }
> i++;
> }
>
> return 0;
> }
> ========================================================================
> (<URL: http://www.programmingsimplified.com/c/source-code/
> c-program-for-prime-number>)
Here's my tweaked version of that:
========================================================================
#include <stdio.h>
int main()
{
int i = 3, count, factor;
printf("Enter the number of prime numbers required\n");
scanf("%d",&count);
if ( count >= 1 )
{
printf("First %d prime numbers are:\n",count);
printf("2\n");
}
while (count > 1)
{
/* This is a pretty stupid algorithm */
for ( factor = 2 ; factor <= i - 1 ; factor++ )
if ( i%factor == 0 ) break;
if ( factor == i )
{
printf("%d\n",i);
count--;
}
i++;
}
return 0;
}
========================================================================
Doesn't change the parenthesis count (other than that I dropped an
unnecessary pair of braces; some people would prefer to keep them, but
I find they're quite superfluous), but improves readability. (Why use
a for loop when you could use a simple while?) As to the question of
whether this is more or less readable than the Scheme version... I
guess that partly depends on the reader's relative familiarity with C
and Scheme, but it's crystal clear to me what the C version is doing -
and that it's doing something stupid. I don't find it more readable to
cast something as recursive; compare these two tight loops:
(let find-divisor ((c 2))
(cond
((= c i)
(format #t "~S\n" i)
(display-primes (1+ count) (1+ i)))
((= (remainder i c) 0)
(display-primes count (1+ i)))
(else
(find-divisor (1+ c)))))))))
for ( factor = 2 ; factor <= i - 1 ; factor++ )
if ( i%factor == 0 ) break;
if ( factor == i )
{
printf("%d\n",i);
count--;
}
In the first one, you start doing something, and if you don't have a
termination point, you recurse - which means you have to name this
loop as a function. In the second, you simply iterate, and then at the
end, decide whether you have the termination condition or not. It's
easy to see what the loop condition is; it's easy to see that it will
always end by one or other termination rule, and then it acts based on
that. Actually, if you switch the conditions, it would look a bit more
like the Scheme version:
for ( factor = 2 ; i%factor ; factor++ )
{
if ( factor == i )
{
printf("%d\n",i);
count--;
break;
}
}
I wouldn't say this makes the code notably more readable, and it
doesn't change the parenthesis count (apart from making more people
want to put the outer braces in - they're still technically optional,
and that was only an optional reduction in the first place), but it's
a closer equivalent and that might make comparison easier.
My view is definitely that the C version is WAY more readable than the
Scheme one.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
unicode as valid naming symbols Mark H Harris <harrismh777@gmail.com> - 2014-03-25 13:30 -0500
Re: unicode as valid naming symbols wxjmfauth@gmail.com - 2014-03-25 11:52 -0700
Re: unicode as valid naming symbols Mark H Harris <harrismh777@gmail.com> - 2014-03-25 14:24 -0500
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-03-25 19:16 -0700
Re: unicode as valid naming symbols MRAB <python@mrabarnett.plus.com> - 2014-03-25 19:24 +0000
Re: unicode as valid naming symbols Mark H Harris <harrismh777@gmail.com> - 2014-03-25 14:29 -0500
Re: unicode as valid naming symbols Marko Rauhamaa <marko@pacujo.net> - 2014-03-25 21:48 +0200
Re: unicode as valid naming symbols Skip Montanaro <skip@pobox.com> - 2014-03-25 14:54 -0500
Re: unicode as valid naming symbols Cameron Simpson <cs@zip.com.au> - 2014-03-26 09:16 +1100
Re: unicode as valid naming symbols Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-25 13:49 -0600
Re: unicode as valid naming symbols Tim Chase <python.list@tim.thechases.com> - 2014-03-25 15:29 -0500
Re: unicode as valid naming symbols Ethan Furman <ethan@stoneleaf.us> - 2014-03-25 15:47 -0700
Re: unicode as valid naming symbols Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-25 23:58 +0000
Re: unicode as valid naming symbols Mark H Harris <harrismh777@gmail.com> - 2014-03-27 10:28 -0500
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-03-27 08:51 -0700
Re: unicode as valid naming symbols Mark H Harris <harrismh777@gmail.com> - 2014-03-27 11:03 -0500
Re: unicode as valid naming symbols Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-28 12:45 +1300
Re: unicode as valid naming symbols MRAB <python@mrabarnett.plus.com> - 2014-03-27 17:17 +0000
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-03-27 10:53 -0700
Re: unicode as valid naming symbols Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-27 10:22 -0600
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-03-27 10:41 -0700
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-03-28 03:23 +1100
Re: unicode as valid naming symbols Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-31 11:55 +0200
Re: unicode as valid naming symbols Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-31 11:40 -0600
Re: unicode as valid naming symbols Tim Chase <python.list@tim.thechases.com> - 2014-03-31 13:02 -0500
Re: unicode as valid naming symbols Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-31 12:10 -0600
Re: unicode as valid naming symbols Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-31 21:31 +0200
Re: unicode as valid naming symbols Terry Reedy <tjreedy@udel.edu> - 2014-03-31 16:12 -0400
Re: unicode as valid naming symbols Terry Reedy <tjreedy@udel.edu> - 2014-03-31 16:15 -0400
Re: unicode as valid naming symbols Marko Rauhamaa <marko@pacujo.net> - 2014-03-31 23:34 +0300
Re: unicode as valid naming symbols Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-31 18:47 -0600
Re: unicode as valid naming symbols David Hutto <dwightdhutto@gmail.com> - 2014-03-31 23:58 -0400
Re: unicode as valid naming symbols David Hutto <dwightdhutto@gmail.com> - 2014-04-01 00:11 -0400
Re: unicode as valid naming symbols Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-04-01 10:19 +0200
Re: unicode as valid naming symbols Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 03:18 -0600
Re: unicode as valid naming symbols Marko Rauhamaa <marko@pacujo.net> - 2014-04-01 12:32 +0300
Re: unicode as valid naming symbols Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 03:58 -0600
Re: unicode as valid naming symbols Marko Rauhamaa <marko@pacujo.net> - 2014-04-01 15:02 +0300
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-04-01 23:54 +1100
Re: unicode as valid naming symbols Marko Rauhamaa <marko@pacujo.net> - 2014-04-01 16:16 +0300
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-04-02 00:32 +1100
Re: unicode as valid naming symbols Marko Rauhamaa <marko@pacujo.net> - 2014-04-01 18:59 +0300
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-04-01 19:58 -0700
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-04-01 20:16 -0700
Re: unicode as valid naming symbols Marko Rauhamaa <marko@pacujo.net> - 2014-04-02 08:55 +0300
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-04-01 21:39 +1100
Re: unicode as valid naming symbols Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-04-01 12:37 +0200
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-04-01 21:58 +1100
Re: unicode as valid naming symbols Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-04-01 13:59 +0200
Re: unicode as valid naming symbols Roy Smith <roy@panix.com> - 2014-04-01 08:29 -0400
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-04-02 00:08 +1100
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-04-01 06:34 -0700
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-04-02 00:00 +1100
Re: unicode as valid naming symbols Ned Batchelder <ned@nedbatchelder.com> - 2014-04-01 09:33 -0400
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-04-02 00:44 +1100
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-04-01 06:58 -0700
Re: unicode as valid naming symbols Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 09:53 -0600
Re: unicode as valid naming symbols MRAB <python@mrabarnett.plus.com> - 2014-03-26 02:56 +0000
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-03-26 14:09 +1100
Re: unicode as valid naming symbols Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-26 09:25 +0100
Re: unicode as valid naming symbols Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-26 09:52 +0100
Re: unicode as valid naming symbols Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-26 10:37 -0600
Re: unicode as valid naming symbols Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-27 10:36 +0100
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-03-27 08:10 -0700
Re: unicode as valid naming symbols Tim Chase <python.list@tim.thechases.com> - 2014-03-27 10:34 -0500
Re: unicode as valid naming symbols random832@fastmail.us - 2014-03-28 14:55 -0400
Re: unicode as valid naming symbols Rustom Mody <rustompmody@gmail.com> - 2014-03-28 22:00 -0700
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-03-29 16:12 +1100
Re: unicode as valid naming symbols Ben Finney <ben+python@benfinney.id.au> - 2014-03-29 16:32 +1100
Re: unicode as valid naming symbols Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-03-29 14:11 -0400
Re: unicode as valid naming symbols Chris Angelico <rosuav@gmail.com> - 2014-03-30 09:01 +1100
Re: unicode as valid naming symbols Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-30 19:16 +1300
Re: unicode as valid naming symbols Mark H Harris <harrismh777@gmail.com> - 2014-03-25 14:29 -0500
Re:unicode as valid naming symbols Dave Angel <davea@davea.name> - 2014-03-25 15:45 -0400
Re: unicode as valid naming symbols Terry Reedy <tjreedy@udel.edu> - 2014-03-25 22:26 -0400
csiph-web