-1

I am trying to make a code (in python) where I can input a range and it will find the sum off all the numbers besides the ones that are divisible by x (which i also choose).

For example:

if the range is 0<N<10 and x = 3 then I want the code to sum the numbers 1 + 2 + 4 + 5 + 7 + 8 and output 27.

or if the range is 0<N<5 and x = 2 I want the code to sum the numbers 1 + 3 and output 4

But, the problem is I have no idea how to do it. Can anyone help me?

Gabe
  • 84,912
  • 12
  • 139
  • 238
Zahand
  • 490
  • 3
  • 12
  • 23
  • 5
    Well, start with *something*. Do you know how to create a range of numbers? How about filtering out multiples of a certain number? Summing is trivial. – Blender Jan 22 '14 at 08:35
  • Which part are you stuck on? – Gabe Jan 22 '14 at 08:35
  • 1
    How would you, as a human, perform this task? Can you translate that into step-by-step instructions? – user2357112 Jan 22 '14 at 08:36
  • Even though it's a valid question to ask on SO, I suggest you read up basics on the particular programming language first. SO is not the place to comprehensively learn a language, though people will be ready to help you out with that as well. IMHO, you will be better off spending time on the docs than framing a question that adheres to the rules here. Not to mention the unnecessary downvotes! – gravetii Jan 22 '14 at 08:52

4 Answers4

3

For your second example: (0<N<5, x=2):

sum(i for i in range(1, 5) if i%2)
thengineer
  • 555
  • 1
  • 4
  • 14
2
def fn(N, x):
    total = 0
    for i in range(N):
        if i%x:
            total += i
    return total

Read up on loops and ranges in python if you are new.

azro
  • 53,056
  • 7
  • 34
  • 70
gravetii
  • 9,273
  • 9
  • 56
  • 75
  • 3
    Tempted to downvote because of `is not 0`. At the very least, that should be `!= 0` - and since any number except `0` is "truthy", it can be dropped altogether: `if i%x:`... – Tim Pietzcker Jan 22 '14 at 08:38
  • Agree with you about the latter (edited answer) but why do you say is not 0 is wrong? The reason I chose to write it explicitly is considering the fact that OP is new to python. :-) – gravetii Jan 22 '14 at 08:40
  • 1
    @gravetii `is` is checking for object ids and not equality – Loïc Faure-Lacroix Jan 22 '14 at 08:41
  • @gravetii: Run the following 3 statements in the interactive interpreter, on separate lines: `x = 1000`, `y = 1000`, `x is y`. Then read [this existing StackOverflow question](http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers). – user2357112 Jan 22 '14 at 08:41
  • 1
    `is` is used to compare for identity, not equality. Now there is a small set of numbers where the two behave the same, but this is an implementation-dependent detail and can't be relied upon. – Tim Pietzcker Jan 22 '14 at 08:41
  • @user2357112 do that `x=1000; y=1000` and `x is y` and behold! – Loïc Faure-Lacroix Jan 22 '14 at 08:42
  • @TimPietzcker You are right. Thanks for adding the comment, edited accordingly. – gravetii Jan 22 '14 at 08:42
  • 1
    @LoïcFaure-Lacroix: That's why I said separate lines. – user2357112 Jan 22 '14 at 08:42
  • Good answer, nicely explained, should be comprehensible to even a novice… but one minor quibble: The OP wanted `0 – abarnert Jan 22 '14 at 08:47
  • @LoïcFaure-Lacroix: For real fun, put the multi-line version into a module or function and import or call it. :) – abarnert Jan 22 '14 at 08:50
  • @abarnert Yes, that's why this behaviour shouldn't be used. As far as I understand, it's depends on how the code is compiled. If the code is compiled together it will be able to optimize "duplicates" of immutable variables. The REPL will compile each line separately that is why the one line works and the two lines don't. Modules/Functions are all compiled in one time for this reason it should also work but if python would decide to compile chunks of code in a module seprately it wouldn't work 100%. – Loïc Faure-Lacroix Jan 22 '14 at 08:57
  • @LoïcFaure-Lacroix: Exactly. As an aside, clever idea on compiling chunks separately… I wonder if you could get any useful optimizations that way by noting that certain variables are only accessed in certain chunks (to reduce the number of locals, free things earlier, etc.)? – abarnert Jan 22 '14 at 09:05
  • @abarnert I'd say maybe and maybe not. The interpreter should make the right decisions on that matter. – Loïc Faure-Lacroix Jan 22 '14 at 09:41
  • @LoïcFaure-Lacroix: Well, the CPython interpreter never frees things that are still in scope, and I don't know that it could (because there's no way to be sure you won't use them again without peeking at upcoming code), and it definitely couldn't reuse local slots (because the compiler creates those slots, and the interpreter just refers to them by index)… but anyway, we're getting way off topic here and probably abusing the comments section. I'll file your idea away to think about later instead of derailing gravetii's nice answer. – abarnert Jan 22 '14 at 19:39
1

You could do something like this:

>>> div = 3
>>> n = 10 
>>> num_div = filter(lambda x: x%div, range(n))
>>> sum(num_div)
27

or as a function

def func(n,div):
   return sum(filter(lambda x: x%div, range(n))
greole
  • 4,523
  • 5
  • 29
  • 49
0

The other answers implicitly assume the range will always start from 0. If you want to be able to set both the start and end points of your range, you could use:

def sumrange(start, stop, n):
    return sum(i for i in range(start, stop) if i%n)
BioGeek
  • 21,897
  • 23
  • 83
  • 145