Python Forum
Divide a number - Multiple levels - Sum of all numbers equal to input number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Divide a number - Multiple levels - Sum of all numbers equal to input number
#11
(Mar-25-2018, 11:31 AM)pythoneer Wrote: could you please explain this a bit for my understanding
This is a relatively short code. I don't know what you don't understand. You can perhaps add a few print() statements to trace some values while the program runs.
Reply
#12
yes, got it thanks, how can i use this with range of numbers instead of single number
Reply
#13
it is working , i am trying to understand python through this forum, what will be the code if i want to get the output as follows, the change from the last time is the last column is accumulation of numbers
[inline] 500 125.0 31.25
500 125.0 68.75
500 125.0 125.0
500 150.0 170
500 150.0 237.5
500 150.0 275
500 225.0 376.25
500 225.0 432.5
500 225.0 500[/inline]
Reply
#14
One only needs a small change in the code
def gen_result(number, divisors):
    total = sum(divisors)
    acc = 0
    for i, d in enumerate(divisors):
        num2 = number * d / total
        for d2 in divisors[i:] + divisors[:i]:
            acc += num2 * d2 / total
            yield [number, num2, acc]
                
for item in gen_result(500, [5, 6, 9]):
    print('{} {} {}'.format(*item))
Reply
#15
wow, it works well, i am slowly understanding python now. What should i do if i want to do the same operation in range of numbers range(500,504,2) with each range ending with that range number and the next range should start adding from that number
Reply
#16
any help on this?
Reply
#17
Any help on how to get this code working with range?
Reply
#18
Any help on how to get this code working with range?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create sum clusters of a number sequence BramQBIC 7 1,015 Nov-24-2023, 12:09 PM
Last Post: Pedroski55
Bug Program to check whether a number is palindrome or not PythonBoy 18 2,716 Sep-12-2023, 09:27 AM
Last Post: PythonBoy
  Trying to find the next prime number FirstBornAlbratross 8 4,343 Aug-14-2023, 01:16 PM
Last Post: deanhystad
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,422 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Python Program to Find the Factorial of a Number elisahill 2 1,441 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  String to Number in Python Godjuned 3 1,359 Nov-17-2022, 07:22 AM
Last Post: Godjuned
  list digit into number Voldyy 2 1,539 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  number to string Ali_ 1 1,281 Mar-31-2022, 11:22 AM
Last Post: ndc85430
  Checking the number of input Chrilo06 3 2,011 Mar-14-2022, 07:31 PM
Last Post: deanhystad
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,632 Feb-24-2022, 10:40 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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