Using a For Loop to find a sequences expression [1/1+1/2+1/3…1/1000]
I am needing assistance for an assignment. I am needing to use python to figure out how to create a "for Loop" that will do the following:
Series 1/1+1/2+1/3+1/4+...+1/1000 (which is expressed as 1000. ∑ n=1. 1 n ≈. 7.49)
I am needing the program to loop through all of them, printing each number out.
Example:
998 7.483469855949342
999 7.48447086055343
1000 7.485470860550343
The basic what I currently got is
for x in range(1, 1000):
I don't know why but I just struggling to get this equation to work in my head. Any help would be greatly appreciated.
python for-loop math sequence
add a comment |
I am needing assistance for an assignment. I am needing to use python to figure out how to create a "for Loop" that will do the following:
Series 1/1+1/2+1/3+1/4+...+1/1000 (which is expressed as 1000. ∑ n=1. 1 n ≈. 7.49)
I am needing the program to loop through all of them, printing each number out.
Example:
998 7.483469855949342
999 7.48447086055343
1000 7.485470860550343
The basic what I currently got is
for x in range(1, 1000):
I don't know why but I just struggling to get this equation to work in my head. Any help would be greatly appreciated.
python for-loop math sequence
add a comment |
I am needing assistance for an assignment. I am needing to use python to figure out how to create a "for Loop" that will do the following:
Series 1/1+1/2+1/3+1/4+...+1/1000 (which is expressed as 1000. ∑ n=1. 1 n ≈. 7.49)
I am needing the program to loop through all of them, printing each number out.
Example:
998 7.483469855949342
999 7.48447086055343
1000 7.485470860550343
The basic what I currently got is
for x in range(1, 1000):
I don't know why but I just struggling to get this equation to work in my head. Any help would be greatly appreciated.
python for-loop math sequence
I am needing assistance for an assignment. I am needing to use python to figure out how to create a "for Loop" that will do the following:
Series 1/1+1/2+1/3+1/4+...+1/1000 (which is expressed as 1000. ∑ n=1. 1 n ≈. 7.49)
I am needing the program to loop through all of them, printing each number out.
Example:
998 7.483469855949342
999 7.48447086055343
1000 7.485470860550343
The basic what I currently got is
for x in range(1, 1000):
I don't know why but I just struggling to get this equation to work in my head. Any help would be greatly appreciated.
python for-loop math sequence
python for-loop math sequence
edited Nov 25 '18 at 8:51
lagom
3,142102038
3,142102038
asked Nov 25 '18 at 6:10
Anna CollinsAnna Collins
488
488
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You are going in the right direction. Before the for
loop you would require a sum variable, where you would store the value of the summation of 1/x
.
You can do that in a similar way:
sum = 0
for x in range(1, 1001):
sum += (1/x)
print(sum, x)
Here, I have initialized the sum variable to 0. After that, I iterate x over the values of [1, 1000] (both included). I find 1/x
and add it to the sum. Next, I print the values, as you wanted.
NOTE: range(x, y)
method gives you a range from x
to y-1
Bravo! The only issue I see here is that your are storing values into memory. This means slowness if dealing with big data. ;)
– Prayson W. Daniel
Nov 25 '18 at 6:49
1
@PraysonW.Daniel, you are right, however this seems to be beginner's question. We should not impose the effects of sluggishness of storing variables in memory right now (at this level), I guess.
– MaJoR
Nov 25 '18 at 6:54
1
Agree. I think for tutorials purposes, it is okay but were it to be a production code, efficiency is to be preferred.
– Prayson W. Daniel
Nov 25 '18 at 6:57
add a comment |
itertools are your best friend. The proposed answers is correct but would be slow for big data. If I were you I would do:
import itertools
a = map(lambda x:1/x,range(1,1001))
#print(list(itertools.accumulate(a)))
for i, j in enumerate(1,itertools.accumulate(a)):
print(i,j)
Explaination: lambda x:1/x creates on-fly function that would transform n to 1/n. map maps that function to the range of value starting from 1 to 1000. I then pass this to accumulating 1/1+1/2..... ;)
add a comment |
Keep in mind that python2 will return 0 for 1/x and your sum will lead to 1 at the end. For getting float output(i.e. 0.25 for 1/4) one of the numbers have to be converted to float(either 1 or either x). Hence, the right way would be
sum = 0
for x in range(1, 1001):
sum += (float(1)/x)
print(sum, x)
Or you can import division from the future. from __future__ import division. The Python 2 division would be replaced by 3 and business as usual :)
– Prayson W. Daniel
Nov 25 '18 at 6:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465100%2fusing-a-for-loop-to-find-a-sequences-expression-1-11-21-3-1-1000%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are going in the right direction. Before the for
loop you would require a sum variable, where you would store the value of the summation of 1/x
.
You can do that in a similar way:
sum = 0
for x in range(1, 1001):
sum += (1/x)
print(sum, x)
Here, I have initialized the sum variable to 0. After that, I iterate x over the values of [1, 1000] (both included). I find 1/x
and add it to the sum. Next, I print the values, as you wanted.
NOTE: range(x, y)
method gives you a range from x
to y-1
Bravo! The only issue I see here is that your are storing values into memory. This means slowness if dealing with big data. ;)
– Prayson W. Daniel
Nov 25 '18 at 6:49
1
@PraysonW.Daniel, you are right, however this seems to be beginner's question. We should not impose the effects of sluggishness of storing variables in memory right now (at this level), I guess.
– MaJoR
Nov 25 '18 at 6:54
1
Agree. I think for tutorials purposes, it is okay but were it to be a production code, efficiency is to be preferred.
– Prayson W. Daniel
Nov 25 '18 at 6:57
add a comment |
You are going in the right direction. Before the for
loop you would require a sum variable, where you would store the value of the summation of 1/x
.
You can do that in a similar way:
sum = 0
for x in range(1, 1001):
sum += (1/x)
print(sum, x)
Here, I have initialized the sum variable to 0. After that, I iterate x over the values of [1, 1000] (both included). I find 1/x
and add it to the sum. Next, I print the values, as you wanted.
NOTE: range(x, y)
method gives you a range from x
to y-1
Bravo! The only issue I see here is that your are storing values into memory. This means slowness if dealing with big data. ;)
– Prayson W. Daniel
Nov 25 '18 at 6:49
1
@PraysonW.Daniel, you are right, however this seems to be beginner's question. We should not impose the effects of sluggishness of storing variables in memory right now (at this level), I guess.
– MaJoR
Nov 25 '18 at 6:54
1
Agree. I think for tutorials purposes, it is okay but were it to be a production code, efficiency is to be preferred.
– Prayson W. Daniel
Nov 25 '18 at 6:57
add a comment |
You are going in the right direction. Before the for
loop you would require a sum variable, where you would store the value of the summation of 1/x
.
You can do that in a similar way:
sum = 0
for x in range(1, 1001):
sum += (1/x)
print(sum, x)
Here, I have initialized the sum variable to 0. After that, I iterate x over the values of [1, 1000] (both included). I find 1/x
and add it to the sum. Next, I print the values, as you wanted.
NOTE: range(x, y)
method gives you a range from x
to y-1
You are going in the right direction. Before the for
loop you would require a sum variable, where you would store the value of the summation of 1/x
.
You can do that in a similar way:
sum = 0
for x in range(1, 1001):
sum += (1/x)
print(sum, x)
Here, I have initialized the sum variable to 0. After that, I iterate x over the values of [1, 1000] (both included). I find 1/x
and add it to the sum. Next, I print the values, as you wanted.
NOTE: range(x, y)
method gives you a range from x
to y-1
answered Nov 25 '18 at 6:16
MaJoRMaJoR
534115
534115
Bravo! The only issue I see here is that your are storing values into memory. This means slowness if dealing with big data. ;)
– Prayson W. Daniel
Nov 25 '18 at 6:49
1
@PraysonW.Daniel, you are right, however this seems to be beginner's question. We should not impose the effects of sluggishness of storing variables in memory right now (at this level), I guess.
– MaJoR
Nov 25 '18 at 6:54
1
Agree. I think for tutorials purposes, it is okay but were it to be a production code, efficiency is to be preferred.
– Prayson W. Daniel
Nov 25 '18 at 6:57
add a comment |
Bravo! The only issue I see here is that your are storing values into memory. This means slowness if dealing with big data. ;)
– Prayson W. Daniel
Nov 25 '18 at 6:49
1
@PraysonW.Daniel, you are right, however this seems to be beginner's question. We should not impose the effects of sluggishness of storing variables in memory right now (at this level), I guess.
– MaJoR
Nov 25 '18 at 6:54
1
Agree. I think for tutorials purposes, it is okay but were it to be a production code, efficiency is to be preferred.
– Prayson W. Daniel
Nov 25 '18 at 6:57
Bravo! The only issue I see here is that your are storing values into memory. This means slowness if dealing with big data. ;)
– Prayson W. Daniel
Nov 25 '18 at 6:49
Bravo! The only issue I see here is that your are storing values into memory. This means slowness if dealing with big data. ;)
– Prayson W. Daniel
Nov 25 '18 at 6:49
1
1
@PraysonW.Daniel, you are right, however this seems to be beginner's question. We should not impose the effects of sluggishness of storing variables in memory right now (at this level), I guess.
– MaJoR
Nov 25 '18 at 6:54
@PraysonW.Daniel, you are right, however this seems to be beginner's question. We should not impose the effects of sluggishness of storing variables in memory right now (at this level), I guess.
– MaJoR
Nov 25 '18 at 6:54
1
1
Agree. I think for tutorials purposes, it is okay but were it to be a production code, efficiency is to be preferred.
– Prayson W. Daniel
Nov 25 '18 at 6:57
Agree. I think for tutorials purposes, it is okay but were it to be a production code, efficiency is to be preferred.
– Prayson W. Daniel
Nov 25 '18 at 6:57
add a comment |
itertools are your best friend. The proposed answers is correct but would be slow for big data. If I were you I would do:
import itertools
a = map(lambda x:1/x,range(1,1001))
#print(list(itertools.accumulate(a)))
for i, j in enumerate(1,itertools.accumulate(a)):
print(i,j)
Explaination: lambda x:1/x creates on-fly function that would transform n to 1/n. map maps that function to the range of value starting from 1 to 1000. I then pass this to accumulating 1/1+1/2..... ;)
add a comment |
itertools are your best friend. The proposed answers is correct but would be slow for big data. If I were you I would do:
import itertools
a = map(lambda x:1/x,range(1,1001))
#print(list(itertools.accumulate(a)))
for i, j in enumerate(1,itertools.accumulate(a)):
print(i,j)
Explaination: lambda x:1/x creates on-fly function that would transform n to 1/n. map maps that function to the range of value starting from 1 to 1000. I then pass this to accumulating 1/1+1/2..... ;)
add a comment |
itertools are your best friend. The proposed answers is correct but would be slow for big data. If I were you I would do:
import itertools
a = map(lambda x:1/x,range(1,1001))
#print(list(itertools.accumulate(a)))
for i, j in enumerate(1,itertools.accumulate(a)):
print(i,j)
Explaination: lambda x:1/x creates on-fly function that would transform n to 1/n. map maps that function to the range of value starting from 1 to 1000. I then pass this to accumulating 1/1+1/2..... ;)
itertools are your best friend. The proposed answers is correct but would be slow for big data. If I were you I would do:
import itertools
a = map(lambda x:1/x,range(1,1001))
#print(list(itertools.accumulate(a)))
for i, j in enumerate(1,itertools.accumulate(a)):
print(i,j)
Explaination: lambda x:1/x creates on-fly function that would transform n to 1/n. map maps that function to the range of value starting from 1 to 1000. I then pass this to accumulating 1/1+1/2..... ;)
edited Nov 25 '18 at 6:47
answered Nov 25 '18 at 6:41
Prayson W. DanielPrayson W. Daniel
2,10111319
2,10111319
add a comment |
add a comment |
Keep in mind that python2 will return 0 for 1/x and your sum will lead to 1 at the end. For getting float output(i.e. 0.25 for 1/4) one of the numbers have to be converted to float(either 1 or either x). Hence, the right way would be
sum = 0
for x in range(1, 1001):
sum += (float(1)/x)
print(sum, x)
Or you can import division from the future. from __future__ import division. The Python 2 division would be replaced by 3 and business as usual :)
– Prayson W. Daniel
Nov 25 '18 at 6:53
add a comment |
Keep in mind that python2 will return 0 for 1/x and your sum will lead to 1 at the end. For getting float output(i.e. 0.25 for 1/4) one of the numbers have to be converted to float(either 1 or either x). Hence, the right way would be
sum = 0
for x in range(1, 1001):
sum += (float(1)/x)
print(sum, x)
Or you can import division from the future. from __future__ import division. The Python 2 division would be replaced by 3 and business as usual :)
– Prayson W. Daniel
Nov 25 '18 at 6:53
add a comment |
Keep in mind that python2 will return 0 for 1/x and your sum will lead to 1 at the end. For getting float output(i.e. 0.25 for 1/4) one of the numbers have to be converted to float(either 1 or either x). Hence, the right way would be
sum = 0
for x in range(1, 1001):
sum += (float(1)/x)
print(sum, x)
Keep in mind that python2 will return 0 for 1/x and your sum will lead to 1 at the end. For getting float output(i.e. 0.25 for 1/4) one of the numbers have to be converted to float(either 1 or either x). Hence, the right way would be
sum = 0
for x in range(1, 1001):
sum += (float(1)/x)
print(sum, x)
answered Nov 25 '18 at 6:25
Biswadip MandalBiswadip Mandal
1809
1809
Or you can import division from the future. from __future__ import division. The Python 2 division would be replaced by 3 and business as usual :)
– Prayson W. Daniel
Nov 25 '18 at 6:53
add a comment |
Or you can import division from the future. from __future__ import division. The Python 2 division would be replaced by 3 and business as usual :)
– Prayson W. Daniel
Nov 25 '18 at 6:53
Or you can import division from the future. from __future__ import division. The Python 2 division would be replaced by 3 and business as usual :)
– Prayson W. Daniel
Nov 25 '18 at 6:53
Or you can import division from the future. from __future__ import division. The Python 2 division would be replaced by 3 and business as usual :)
– Prayson W. Daniel
Nov 25 '18 at 6:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465100%2fusing-a-for-loop-to-find-a-sequences-expression-1-11-21-3-1-1000%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown