[Date Index]
[Thread Index]
[Author Index]
RE: question on Sum over selected integers
*To*: mathgroup at smc.vnet.net
*Subject*: [mg43858] RE: [mg43833] question on Sum over selected integers
*From*: "David Park" <djmp at earthlink.net>
*Date*: Thu, 9 Oct 2003 01:54:31 -0400 (EDT)
*Sender*: owner-wri-mathgroup at wolfram.com
Ghassan,
Suppose we want to sum the function f over selected integers.
f[x_] := x^2
Let's do a smaller range from 1 to 10 for the example. Here the sum is
developed in steps.
1) Generate the full range of integers
2) Take the complement with the missing integers
3) Map the function onto the list
4) Apply Plus to convert the List to a sum
Range[10]
Complement[%, {2, 6, 8}]
f /@ %
Plus @@ %
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{1, 3, 4, 5, 7, 9, 10}
{1, 9, 16, 25, 49, 81, 100}
281
We can encapsulate all this into a single function.
specialsum[f_, missing_, n_] := Plus @@ f /@ Complement[Range[n], missing]
specialsum[f, {2, 6, 8}, 10]
281
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: Ghassan Shahin [mailto:ghassans at bethlehem.edu]
To: mathgroup at smc.vnet.net
Hi..
I have a simple question..
i want to use mathematica for sums..but i need to sum from 1 to 100
except same integer numbers, say 2,6,8..what is the code for such a
task?
thank you
Prev by Date:
**Re: Discrete Fourier Transform**
Next by Date:
**Re: Using Symbols in Functions**
Previous by thread:
**Re: Using Symbols in Functions**
Next by thread:
**MathLink: support for long longs?**
| |