Python Forum
[Learning] 3 hours for one simple exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Learning] 3 hours for one simple exercise
#5
(Aug-25-2019, 04:48 AM)ndc85430 Wrote: I don't understand why you need all those variables. Is it not sufficient to have a loop variable that goes up in steps of 4 between the correct bounds (see range()) and a variable to keep track of the sum?

Also, your example contains the numbers 12, 52, 92 and 132, none of which are square.

Sorry google translate automatically resolved the exponentials of the numbers. Those numbers are exponentials... 1**2, 5**2, 9**2,13**2, etc....

Yeah, I want to make a loop that gos in steps of 4 but IDK how.


(Aug-25-2019, 07:41 AM)ThomasL Wrote: Please explain explicitly how these number are calculated: (12 + 52 + 92 + 132 + ...)
Please explain what is meant with this statement: "return the sum of the squares of those numbers between 1 and N that are separated from each other by four units."
What is meant with "4 units"?
Where did you get this exercise? Do you know the solutions for numbers n == 1,2,3,4,5,6 etc?
----------------------------------
Let´s look at the number 1 to 10 and their squares:
1 1, 2 4, 3 9, 4 16, 5 25, 6 36, 7 49, 8 64, 9 81, 10 100
If n == 1: the sum of the square of 1 is 1.
if n == 2: the sum is 1 + 5 = 6
if n == 3: the sum is 1+5+9 = 15
if n == 4: the sum is 1+5+9+16 = 31
if n == 5: the sum is 1+5+9+16+25 = 56
n == 6: 56+36 = 92
n == 7: 92+49 = 141
n == 8: 141+64 = 205
n == 9: 205+81 = 286
n == 10: 286+100 = 386

I don´t find any of those sums in your numbers, so I don´t understand your exercise or the numbers you provided have nothing to do with that exercise.

The exercise is simple if the user inputs the number 10. Is going to search for the number 4 units away from the original and then save the exponent, and keep repeating that until the input number reaches 0.
Ex: 10, 6, 2.
another ex: 5,1
yet another ex: 12,8,4


(Aug-25-2019, 10:42 AM)perfringo Wrote: There are some ambiguity (what does mean separated by four) but sum of every forth integer in range n should be simply:

>>> n = 100
>>> sum(i ** 2 for i in range(1, n+1, 4))
80825

Yeah! that's what I'm looking for! I'm gonna try that solution! Thanks!
Reply


Messages In This Thread
RE: [Learning] 3 hours for one simple exercise - by KoFu - Aug-25-2019, 03:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Plz help Ive tried for hours and cant do these three problems VB4 1 2,003 Mar-17-2018, 04:59 AM
Last Post: micseydel
  Simple exercise - how to write in python tomtom55 6 5,028 Sep-28-2017, 07:18 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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