Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
accumulator pattern
#1
Write a programm which sums up the first 100 even numbers. + comments
Reply
#2
Show us what have you done so far.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
x=2+4+6+8+[....]+198+200
Filling the dots is left as an exercise to the reader.

If you are mathematically inclined:
x=2*((100*101)*2)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
Somehow I doubt that it will be accepted as a satisfactory solution of programming homework (even if OP's name is Carl Gauss). The assignment is not too precise, so there is a question of allowing zero.

Last * probably means /.
Reply
#5
(Mar-09-2017, 11:20 PM)zivoni Wrote: The assignment is not too precise, so there is a question of allowing zero.

Output:
>>> sum([x for x in range(2,102,2)]) == sum([x for x in range(0,102,2)]) True
it gets the same sum, either way.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Mar-09-2017, 11:20 PM)zivoni Wrote: Somehow I doubt that it will be accepted as a satisfactory solution of programming homework (even if OP's name is Carl Gauss). The assignment is not too precise, so there is a question of allowing zero.
A philosophy teacher would have excuses, but not a programming teacher.

Quote:Last * probably means /.
Yes
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#7
I was thinking about some "pragmatic" teacher -  one who have just explained for (or while) loop in a class and expects to see it used in the following homework.

Quote:
Output:
>>> sum([x for x in range(2,102,2)]) == sum([x for x in range(0,102,2)]) True
it gets the same sum, either way.
The right expression is a sum of one more number than the left one.

But yes, it was nitpicking. Anyone who is able to sum numbers from 1 to 100 should be able to sum numbers from 0 to 99.

EDIT: I have misunderstood Ofnuts' comment - it was about zero, not about accepting solution given by a single expression. And he is right, it shouldnt matter from the programming point of view.
Reply
#8
(Mar-10-2017, 09:36 AM)zivoni Wrote: I was thinking about some "pragmatic" teacher -  one who have just explained for (or while) loop in a class and expects to see it used in the following homework.

Quote:
Output:
>>> sum([x for x in range(2,102,2)]) == sum([x for x in range(0,102,2)]) True
it gets the same sum, either way.
The right expression is a sum of one more number than the left one.

But yes, it was nitpicking. Anyone who is able to sum numbers from 1 to 100 should be able to sum numbers from 0 to 99.

EDIT: I have misunderstood Ofnuts' comment - it was about zero, not about accepting solution given by a single expression. And he is right, it shouldnt matter from the programming point of view.

No, your first understanding was the correct one :). If you teach computer science you have to be accurate. What the specs don't forbid is allowed. Nothing should be "implicit". A long time ago I worked on a proposal for a device automatically calling for assistance in case of a car crash. The car speed was sampled at 2kHz or so, so the acceleration was the difference between two consecutive samples and the "crash" itself was detected when the average of the last 200 accelerations was above some value. Computing this fast enough wasn't that easy on the available microcomputers of the era. Until you figure out that the average of all the deltas is just the last value minus the first value divided by the number of samples.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#9
OK :)

Yes, OP's demand for program and comment is fullfilled by
print(50*202)  # sums even numbers from 2 to 200
But if his post is not a precise transcription of homework specs, then there is possibility that OP gets what he asks for, not what he wants. And without his cooperation its rhetorical question.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Accumulator/looping problem student8 5 4,375 Oct-16-2017, 01:24 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020