Im just started in python, trying to solve this:
You need to place " " between numbers 123...N, to have M as a result
Example:
In: 5 15 Out: 1 2 3 4 5=15
In: 4 46 Out: 12 34=46
I was searchin all over enthernet, but found none =-(
CodePudding user response:
Here is a simple algorithm that you can use to sum N numbers and place the " " signs between them to get a specific result:
- Start by defining the result you want to get (M), and the number of numbers you want to sum (N).
- Create a list of numbers from 1 to N, which will be the numbers you want to sum.
- Set a variable current_sum to 0, which will be used to track the current sum of the numbers.
- Use a loop to iterate over the list of numbers. In each iteration of the loop, add the current number to current_sum and remove it from the list of numbers.
- After adding the number to current_sum, check if current_sum is equal to M. If it is, exit the loop and print the list of numbers that were summed, separated by " " signs.
- If current_sum is not equal to M, continue to the next iteration of the loop and repeat steps 4 and 5 until current_sum is equal to M or the list of numbers is empty.
- If the list of numbers is empty and current_sum is not equal to M, print a message indicating that a solution was not found.
Here is an example of how this algorithm would work for the input 5 15:
- Set M to 15 and N to 5.
- Create a list of numbers from 1 to 5: [1, 2, 3, 4, 5]
- Set current_sum to 0.
- Iterate over the list of numbers:
- Add the first number (1) to current_sum and remove it from the list: current_sum = 1, [2, 3, 4, 5]
- Check if current_sum is equal to M: no, continue
- Add the second number (2) to current_sum and remove it from the list: current_sum = 3, [3, 4, 5]
- Check if current_sum is equal to M: no, continue
- Add the third number (3) to current_sum and remove it from the list: current_sum = 6, [4, 5]
- Check if current_sum is equal to M: no, continue
- Add the fourth number (4) to current_sum and remove it from the list: current_sum = 10, [5]
- Check if current_sum is equal to M: no, continue
- Add the fifth number (5) to current_sum and remove it from the list: current_sum = 15, []
- Check if current_sum is equal to M: yes, exit the loop
- Print the list of numbers that were summed, separated by " " signs: "1 2 3 4 5=15"
This algorithm will work for any values of M and N, as long as a solution exists. It may not find a solution if one does not exist, but it will always find a solution if one does exist
CodePudding user response:
Simple readable code.. Hope you like it!!
Code:-
n=input("Input number: ")
target=input("Input the target value: ")
total=0
lis=[str(i) for i in range(1,int(n) 1)]
flag=True
#print(lis)
for i in range(len(lis)):
#print(lis[:i],lis[i:])
left_side="".join(lis[:i])
#print(left_side)
right_side="".join(lis[i:])
#print(right_side)
temp=left_side " " right_side
total=eval(temp)
#print(total)
if total==int(target):
print(left_side " " right_side " gives output: " target)
flag=False
break
if flag: #No target value found
print("There is no value which sum up to target")
Output:- #Testcase1: When Target is achievable.
Input number: 4
Input the target value: 46
12 34 gives output: 46
#Testcase2: When Target is not achievable
Input number: 8
Input the target value: 345
There is no value which sum up to target