Compute the ratio of the sum of cube of the first 'n' natural numbers to the sum of square of first 'n' natural numbersWhy are Python lambdas useful?Convert a number range to another range, maintaining ratioPython progression path - From apprentice to guruTaking square of summed numbersFunction to calculate the difference between sum of squares and square of sumsWhy is “1000000000000000 in range(1000000000000001)” so fast in Python 3?Sum of squares using lambda functionssum of squares of the first n odd numbersalgorithm to get power sum of natural numbersCompute cube root of extremely big number in Python3
What is the philosophical significance of speech acts/implicature?
How to stop co-workers from teasing me because I know Russian?
How to display Aura JS Errors Lightning Out
What happens to Mjolnir (Thor's hammer) at the end of Endgame?
How can I print the prosodic symbols in LaTeX?
What happened to Captain America in Endgame?
How can I practically buy stocks?
Which big number is bigger?
What makes accurate emulation of old systems a difficult task?
Alignment of various blocks in tikz
How to fry ground beef so it is well-browned
Pulling the rope with one hand is as heavy as with two hands?
Multiple options vs single option UI
"The cow" OR "a cow" OR "cows" in this context
Implications of cigar-shaped bodies having rings?
A Note on N!
Can we say “you can pay when the order gets ready”?
What is causing the white spot to appear in some of my pictures
As an international instructor, should I openly talk about my accent?
Was there a Viking Exchange as well as a Columbian one?
Is there a way to generate a list of distinct numbers such that no two subsets ever have an equal sum?
Can an Area of Effect spell cast outside a Prismatic Wall extend inside it?
How to prevent z-fighting in OpenSCAD?
Could the terminal length of components like resistors be reduced?
Compute the ratio of the sum of cube of the first 'n' natural numbers to the sum of square of first 'n' natural numbers
Why are Python lambdas useful?Convert a number range to another range, maintaining ratioPython progression path - From apprentice to guruTaking square of summed numbersFunction to calculate the difference between sum of squares and square of sumsWhy is “1000000000000000 in range(1000000000000001)” so fast in Python 3?Sum of squares using lambda functionssum of squares of the first n odd numbersalgorithm to get power sum of natural numbersCompute cube root of extremely big number in Python3
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Compute the ratio of the sum of cube of the first 'n' natural numbers to the sum of square of first 'n' natural numbers.
Code:
input_int=[x for x in range (1,input_int+1)]
python=reduce(lambda x,y:x+y **3/sum(input_int) % x +y**2/sum(input_int),input_int)
x=10
Solution output:
62.96363636363636
Expected output:
7.857142857142857
python python-3.x
add a comment |
Compute the ratio of the sum of cube of the first 'n' natural numbers to the sum of square of first 'n' natural numbers.
Code:
input_int=[x for x in range (1,input_int+1)]
python=reduce(lambda x,y:x+y **3/sum(input_int) % x +y**2/sum(input_int),input_int)
x=10
Solution output:
62.96363636363636
Expected output:
7.857142857142857
python python-3.x
Please add some context to your question as well as formatting your code.
– Tobias Wilfert
Mar 9 at 8:18
You are only squaring and cubing they
.. for starters. Dividing throughsum(input_int)
makes no sense at all for your taks.
– Patrick Artner
Mar 9 at 9:41
Reads like a university/college/interview programming assignment...
– dijksterhuis
Mar 9 at 12:43
add a comment |
Compute the ratio of the sum of cube of the first 'n' natural numbers to the sum of square of first 'n' natural numbers.
Code:
input_int=[x for x in range (1,input_int+1)]
python=reduce(lambda x,y:x+y **3/sum(input_int) % x +y**2/sum(input_int),input_int)
x=10
Solution output:
62.96363636363636
Expected output:
7.857142857142857
python python-3.x
Compute the ratio of the sum of cube of the first 'n' natural numbers to the sum of square of first 'n' natural numbers.
Code:
input_int=[x for x in range (1,input_int+1)]
python=reduce(lambda x,y:x+y **3/sum(input_int) % x +y**2/sum(input_int),input_int)
x=10
Solution output:
62.96363636363636
Expected output:
7.857142857142857
python python-3.x
python python-3.x
edited Mar 9 at 9:30
Patrick Artner
27.2k62544
27.2k62544
asked Mar 9 at 8:16
Pradeep vPradeep v
1
1
Please add some context to your question as well as formatting your code.
– Tobias Wilfert
Mar 9 at 8:18
You are only squaring and cubing they
.. for starters. Dividing throughsum(input_int)
makes no sense at all for your taks.
– Patrick Artner
Mar 9 at 9:41
Reads like a university/college/interview programming assignment...
– dijksterhuis
Mar 9 at 12:43
add a comment |
Please add some context to your question as well as formatting your code.
– Tobias Wilfert
Mar 9 at 8:18
You are only squaring and cubing they
.. for starters. Dividing throughsum(input_int)
makes no sense at all for your taks.
– Patrick Artner
Mar 9 at 9:41
Reads like a university/college/interview programming assignment...
– dijksterhuis
Mar 9 at 12:43
Please add some context to your question as well as formatting your code.
– Tobias Wilfert
Mar 9 at 8:18
Please add some context to your question as well as formatting your code.
– Tobias Wilfert
Mar 9 at 8:18
You are only squaring and cubing the
y
.. for starters. Dividing through sum(input_int)
makes no sense at all for your taks.– Patrick Artner
Mar 9 at 9:41
You are only squaring and cubing the
y
.. for starters. Dividing through sum(input_int)
makes no sense at all for your taks.– Patrick Artner
Mar 9 at 9:41
Reads like a university/college/interview programming assignment...
– dijksterhuis
Mar 9 at 12:43
Reads like a university/college/interview programming assignment...
– dijksterhuis
Mar 9 at 12:43
add a comment |
2 Answers
2
active
oldest
votes
Your code is very hard to understand and does not even run.
Based on your description of the goal, I can suggest a simpler code to achieve it:
def calc_ratio(n):
sum_3 = sum(x**3 for x in range(1, n+1))
sum_2 = sum(x**2 for x in range(1, n+1))
return sum_3 / sum_2
This would give these results:
>>> calc_ratio(10)
7.857142857142857
>>> calc_ratio(100)
75.3731343283582
>>> calc_ratio(1000)
750.3748125937032
>>> calc_ratio(10000)
7500.374981250938
>>> calc_ratio(100000)
75000.37499812501
add a comment |
from functools import reduce
import ast,sys
input_int = int(sys.stdin.read())
num_list = [x for x in range(1, input_int+1)]
print(reduce(lambda x, y : x + y ** 3, num_list) / reduce(lambda x, y : x + y ** ,num_list))
2
Please consider adding a description of your code so we know what it does and why.
– Pikachu the Purple Wizard
Mar 9 at 19:33
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%2f55075341%2fcompute-the-ratio-of-the-sum-of-cube-of-the-first-n-natural-numbers-to-the-sum%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code is very hard to understand and does not even run.
Based on your description of the goal, I can suggest a simpler code to achieve it:
def calc_ratio(n):
sum_3 = sum(x**3 for x in range(1, n+1))
sum_2 = sum(x**2 for x in range(1, n+1))
return sum_3 / sum_2
This would give these results:
>>> calc_ratio(10)
7.857142857142857
>>> calc_ratio(100)
75.3731343283582
>>> calc_ratio(1000)
750.3748125937032
>>> calc_ratio(10000)
7500.374981250938
>>> calc_ratio(100000)
75000.37499812501
add a comment |
Your code is very hard to understand and does not even run.
Based on your description of the goal, I can suggest a simpler code to achieve it:
def calc_ratio(n):
sum_3 = sum(x**3 for x in range(1, n+1))
sum_2 = sum(x**2 for x in range(1, n+1))
return sum_3 / sum_2
This would give these results:
>>> calc_ratio(10)
7.857142857142857
>>> calc_ratio(100)
75.3731343283582
>>> calc_ratio(1000)
750.3748125937032
>>> calc_ratio(10000)
7500.374981250938
>>> calc_ratio(100000)
75000.37499812501
add a comment |
Your code is very hard to understand and does not even run.
Based on your description of the goal, I can suggest a simpler code to achieve it:
def calc_ratio(n):
sum_3 = sum(x**3 for x in range(1, n+1))
sum_2 = sum(x**2 for x in range(1, n+1))
return sum_3 / sum_2
This would give these results:
>>> calc_ratio(10)
7.857142857142857
>>> calc_ratio(100)
75.3731343283582
>>> calc_ratio(1000)
750.3748125937032
>>> calc_ratio(10000)
7500.374981250938
>>> calc_ratio(100000)
75000.37499812501
Your code is very hard to understand and does not even run.
Based on your description of the goal, I can suggest a simpler code to achieve it:
def calc_ratio(n):
sum_3 = sum(x**3 for x in range(1, n+1))
sum_2 = sum(x**2 for x in range(1, n+1))
return sum_3 / sum_2
This would give these results:
>>> calc_ratio(10)
7.857142857142857
>>> calc_ratio(100)
75.3731343283582
>>> calc_ratio(1000)
750.3748125937032
>>> calc_ratio(10000)
7500.374981250938
>>> calc_ratio(100000)
75000.37499812501
answered Mar 9 at 11:23
RalfRalf
7,55341438
7,55341438
add a comment |
add a comment |
from functools import reduce
import ast,sys
input_int = int(sys.stdin.read())
num_list = [x for x in range(1, input_int+1)]
print(reduce(lambda x, y : x + y ** 3, num_list) / reduce(lambda x, y : x + y ** ,num_list))
2
Please consider adding a description of your code so we know what it does and why.
– Pikachu the Purple Wizard
Mar 9 at 19:33
add a comment |
from functools import reduce
import ast,sys
input_int = int(sys.stdin.read())
num_list = [x for x in range(1, input_int+1)]
print(reduce(lambda x, y : x + y ** 3, num_list) / reduce(lambda x, y : x + y ** ,num_list))
2
Please consider adding a description of your code so we know what it does and why.
– Pikachu the Purple Wizard
Mar 9 at 19:33
add a comment |
from functools import reduce
import ast,sys
input_int = int(sys.stdin.read())
num_list = [x for x in range(1, input_int+1)]
print(reduce(lambda x, y : x + y ** 3, num_list) / reduce(lambda x, y : x + y ** ,num_list))
from functools import reduce
import ast,sys
input_int = int(sys.stdin.read())
num_list = [x for x in range(1, input_int+1)]
print(reduce(lambda x, y : x + y ** 3, num_list) / reduce(lambda x, y : x + y ** ,num_list))
edited Mar 9 at 19:20
Justice_Lords
732211
732211
answered Mar 9 at 12:33
Pradeep vPradeep v
1
1
2
Please consider adding a description of your code so we know what it does and why.
– Pikachu the Purple Wizard
Mar 9 at 19:33
add a comment |
2
Please consider adding a description of your code so we know what it does and why.
– Pikachu the Purple Wizard
Mar 9 at 19:33
2
2
Please consider adding a description of your code so we know what it does and why.
– Pikachu the Purple Wizard
Mar 9 at 19:33
Please consider adding a description of your code so we know what it does and why.
– Pikachu the Purple Wizard
Mar 9 at 19:33
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%2f55075341%2fcompute-the-ratio-of-the-sum-of-cube-of-the-first-n-natural-numbers-to-the-sum%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
Please add some context to your question as well as formatting your code.
– Tobias Wilfert
Mar 9 at 8:18
You are only squaring and cubing the
y
.. for starters. Dividing throughsum(input_int)
makes no sense at all for your taks.– Patrick Artner
Mar 9 at 9:41
Reads like a university/college/interview programming assignment...
– dijksterhuis
Mar 9 at 12:43