How to divided number into three continuous parts such that the third part is the sum of the other two? [closed]
up vote
-5
down vote
favorite
I am trying to write a python program to determine if the digits of a number can be divided into three continuous parts such that the third part is the sum of the other two. e.g. 9999198 can be divided because 99 + 99 = 198.
The sum will always be the least significant digit.
I am unable to come with approach please help.
I am trying to implement it as treating a digit as a single number like in the above case a 7. Then trying to create all subset of three number which adds to 7 and then use these subsets to find the right one. Like 7 = 2,2,3 so my answer is 99,99,198.
My problem is that how can we efficiently split these number into a subset of 3 number.
python
closed as too broad by jonrsharpe, sacul, Rory Daulton, Stephen Kennedy, gnat Nov 10 at 19:34
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 6 more comments
up vote
-5
down vote
favorite
I am trying to write a python program to determine if the digits of a number can be divided into three continuous parts such that the third part is the sum of the other two. e.g. 9999198 can be divided because 99 + 99 = 198.
The sum will always be the least significant digit.
I am unable to come with approach please help.
I am trying to implement it as treating a digit as a single number like in the above case a 7. Then trying to create all subset of three number which adds to 7 and then use these subsets to find the right one. Like 7 = 2,2,3 so my answer is 99,99,198.
My problem is that how can we efficiently split these number into a subset of 3 number.
python
closed as too broad by jonrsharpe, sacul, Rory Daulton, Stephen Kennedy, gnat Nov 10 at 19:34
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
Did you have a question? This isn't a code writing service.
– jonrsharpe
Nov 10 at 17:06
1
Did you try to do it? If yes please let us know where are you stuck?
– Sanchit Kumar
Nov 10 at 17:07
Sorry, yes I have a question. I am stuck on how to approach this problem.
– user7390332
Nov 10 at 17:08
That's not really a question, and certainly not on topic here. See How to Ask.
– jonrsharpe
Nov 10 at 17:11
@SanchitKumar , I am unable to come with an approach.
– user7390332
Nov 10 at 17:11
|
show 6 more comments
up vote
-5
down vote
favorite
up vote
-5
down vote
favorite
I am trying to write a python program to determine if the digits of a number can be divided into three continuous parts such that the third part is the sum of the other two. e.g. 9999198 can be divided because 99 + 99 = 198.
The sum will always be the least significant digit.
I am unable to come with approach please help.
I am trying to implement it as treating a digit as a single number like in the above case a 7. Then trying to create all subset of three number which adds to 7 and then use these subsets to find the right one. Like 7 = 2,2,3 so my answer is 99,99,198.
My problem is that how can we efficiently split these number into a subset of 3 number.
python
I am trying to write a python program to determine if the digits of a number can be divided into three continuous parts such that the third part is the sum of the other two. e.g. 9999198 can be divided because 99 + 99 = 198.
The sum will always be the least significant digit.
I am unable to come with approach please help.
I am trying to implement it as treating a digit as a single number like in the above case a 7. Then trying to create all subset of three number which adds to 7 and then use these subsets to find the right one. Like 7 = 2,2,3 so my answer is 99,99,198.
My problem is that how can we efficiently split these number into a subset of 3 number.
python
python
edited Nov 10 at 17:31
asked Nov 10 at 17:05
user7390332
14
14
closed as too broad by jonrsharpe, sacul, Rory Daulton, Stephen Kennedy, gnat Nov 10 at 19:34
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by jonrsharpe, sacul, Rory Daulton, Stephen Kennedy, gnat Nov 10 at 19:34
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
Did you have a question? This isn't a code writing service.
– jonrsharpe
Nov 10 at 17:06
1
Did you try to do it? If yes please let us know where are you stuck?
– Sanchit Kumar
Nov 10 at 17:07
Sorry, yes I have a question. I am stuck on how to approach this problem.
– user7390332
Nov 10 at 17:08
That's not really a question, and certainly not on topic here. See How to Ask.
– jonrsharpe
Nov 10 at 17:11
@SanchitKumar , I am unable to come with an approach.
– user7390332
Nov 10 at 17:11
|
show 6 more comments
3
Did you have a question? This isn't a code writing service.
– jonrsharpe
Nov 10 at 17:06
1
Did you try to do it? If yes please let us know where are you stuck?
– Sanchit Kumar
Nov 10 at 17:07
Sorry, yes I have a question. I am stuck on how to approach this problem.
– user7390332
Nov 10 at 17:08
That's not really a question, and certainly not on topic here. See How to Ask.
– jonrsharpe
Nov 10 at 17:11
@SanchitKumar , I am unable to come with an approach.
– user7390332
Nov 10 at 17:11
3
3
Did you have a question? This isn't a code writing service.
– jonrsharpe
Nov 10 at 17:06
Did you have a question? This isn't a code writing service.
– jonrsharpe
Nov 10 at 17:06
1
1
Did you try to do it? If yes please let us know where are you stuck?
– Sanchit Kumar
Nov 10 at 17:07
Did you try to do it? If yes please let us know where are you stuck?
– Sanchit Kumar
Nov 10 at 17:07
Sorry, yes I have a question. I am stuck on how to approach this problem.
– user7390332
Nov 10 at 17:08
Sorry, yes I have a question. I am stuck on how to approach this problem.
– user7390332
Nov 10 at 17:08
That's not really a question, and certainly not on topic here. See How to Ask.
– jonrsharpe
Nov 10 at 17:11
That's not really a question, and certainly not on topic here. See How to Ask.
– jonrsharpe
Nov 10 at 17:11
@SanchitKumar , I am unable to come with an approach.
– user7390332
Nov 10 at 17:11
@SanchitKumar , I am unable to come with an approach.
– user7390332
Nov 10 at 17:11
|
show 6 more comments
1 Answer
1
active
oldest
votes
up vote
-1
down vote
accepted
Here's my solution, it checks all possible combinations of splitting given number in 3 parts and checks whether sum of first two components are equal the third one.
def correct_number(x):
str_nmbr = str(x)
for result_split in range(len(str_nmbr)-2):
part_3 = int(str_nmbr[-result_split-1:])
for components_split in range(len(str_nmbr)-2-result_split):
part_2 = int(str_nmbr[1+components_split: -result_split-1])
part_1 = int(str_nmbr[:components_split+1])
if part_1 + part_2 == part_3:
return True
return False
print(correct_number(9999198)) # True
As author requested here's visual explanation how determining parts for number works, given number "1234567"
1 2 3 4 5 6 7:
- First loop chooses second separator
- 1 2 3 4 5 6|7
- Second loop chooses first one
- 1 2|3 4 5 6|7
- 1 2 3|4 5 6|7
- 1 2 3 4|5 6|7
- 1 2 3 4 5|6|7
- . . . Then we move second second separator 1 step back
- 1 2 3 4 5|6 7
- And we continue on moving first separator
- 1|2 3 4 5|6 7
- 1 2|3 4 5|6 7
- 1 2 3|4 5|6 7
- . . .
@Looioe Thanx for the sol. can u explain the little bit how to print all possible 3 number combination? Like 7 into 2,2,3 and 1,3,3 and so on . I guess you use the same approach.
– user7390332
Nov 10 at 17:23
Also FYI What is a help vampire?
– jonrsharpe
Nov 10 at 17:25
@user7390332 first loop determines place where we split number into components side and result side for example with number of 7 digits we'd have to split it from 2 to the end (2 because we need at least 2 digits for first components). Second loop finds separator for components, we want it to be from 1 to place where starts our result number - 1. It would look like this: 1 2 3 4 5 6 7: First loop chooses second separator 1|2 3 4 5 6|7 1 2|3 4 5 6|7 1 2 3|4 5 6|7 . . .
– Filip Młynarski
Nov 10 at 17:32
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
accepted
Here's my solution, it checks all possible combinations of splitting given number in 3 parts and checks whether sum of first two components are equal the third one.
def correct_number(x):
str_nmbr = str(x)
for result_split in range(len(str_nmbr)-2):
part_3 = int(str_nmbr[-result_split-1:])
for components_split in range(len(str_nmbr)-2-result_split):
part_2 = int(str_nmbr[1+components_split: -result_split-1])
part_1 = int(str_nmbr[:components_split+1])
if part_1 + part_2 == part_3:
return True
return False
print(correct_number(9999198)) # True
As author requested here's visual explanation how determining parts for number works, given number "1234567"
1 2 3 4 5 6 7:
- First loop chooses second separator
- 1 2 3 4 5 6|7
- Second loop chooses first one
- 1 2|3 4 5 6|7
- 1 2 3|4 5 6|7
- 1 2 3 4|5 6|7
- 1 2 3 4 5|6|7
- . . . Then we move second second separator 1 step back
- 1 2 3 4 5|6 7
- And we continue on moving first separator
- 1|2 3 4 5|6 7
- 1 2|3 4 5|6 7
- 1 2 3|4 5|6 7
- . . .
@Looioe Thanx for the sol. can u explain the little bit how to print all possible 3 number combination? Like 7 into 2,2,3 and 1,3,3 and so on . I guess you use the same approach.
– user7390332
Nov 10 at 17:23
Also FYI What is a help vampire?
– jonrsharpe
Nov 10 at 17:25
@user7390332 first loop determines place where we split number into components side and result side for example with number of 7 digits we'd have to split it from 2 to the end (2 because we need at least 2 digits for first components). Second loop finds separator for components, we want it to be from 1 to place where starts our result number - 1. It would look like this: 1 2 3 4 5 6 7: First loop chooses second separator 1|2 3 4 5 6|7 1 2|3 4 5 6|7 1 2 3|4 5 6|7 . . .
– Filip Młynarski
Nov 10 at 17:32
add a comment |
up vote
-1
down vote
accepted
Here's my solution, it checks all possible combinations of splitting given number in 3 parts and checks whether sum of first two components are equal the third one.
def correct_number(x):
str_nmbr = str(x)
for result_split in range(len(str_nmbr)-2):
part_3 = int(str_nmbr[-result_split-1:])
for components_split in range(len(str_nmbr)-2-result_split):
part_2 = int(str_nmbr[1+components_split: -result_split-1])
part_1 = int(str_nmbr[:components_split+1])
if part_1 + part_2 == part_3:
return True
return False
print(correct_number(9999198)) # True
As author requested here's visual explanation how determining parts for number works, given number "1234567"
1 2 3 4 5 6 7:
- First loop chooses second separator
- 1 2 3 4 5 6|7
- Second loop chooses first one
- 1 2|3 4 5 6|7
- 1 2 3|4 5 6|7
- 1 2 3 4|5 6|7
- 1 2 3 4 5|6|7
- . . . Then we move second second separator 1 step back
- 1 2 3 4 5|6 7
- And we continue on moving first separator
- 1|2 3 4 5|6 7
- 1 2|3 4 5|6 7
- 1 2 3|4 5|6 7
- . . .
@Looioe Thanx for the sol. can u explain the little bit how to print all possible 3 number combination? Like 7 into 2,2,3 and 1,3,3 and so on . I guess you use the same approach.
– user7390332
Nov 10 at 17:23
Also FYI What is a help vampire?
– jonrsharpe
Nov 10 at 17:25
@user7390332 first loop determines place where we split number into components side and result side for example with number of 7 digits we'd have to split it from 2 to the end (2 because we need at least 2 digits for first components). Second loop finds separator for components, we want it to be from 1 to place where starts our result number - 1. It would look like this: 1 2 3 4 5 6 7: First loop chooses second separator 1|2 3 4 5 6|7 1 2|3 4 5 6|7 1 2 3|4 5 6|7 . . .
– Filip Młynarski
Nov 10 at 17:32
add a comment |
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
Here's my solution, it checks all possible combinations of splitting given number in 3 parts and checks whether sum of first two components are equal the third one.
def correct_number(x):
str_nmbr = str(x)
for result_split in range(len(str_nmbr)-2):
part_3 = int(str_nmbr[-result_split-1:])
for components_split in range(len(str_nmbr)-2-result_split):
part_2 = int(str_nmbr[1+components_split: -result_split-1])
part_1 = int(str_nmbr[:components_split+1])
if part_1 + part_2 == part_3:
return True
return False
print(correct_number(9999198)) # True
As author requested here's visual explanation how determining parts for number works, given number "1234567"
1 2 3 4 5 6 7:
- First loop chooses second separator
- 1 2 3 4 5 6|7
- Second loop chooses first one
- 1 2|3 4 5 6|7
- 1 2 3|4 5 6|7
- 1 2 3 4|5 6|7
- 1 2 3 4 5|6|7
- . . . Then we move second second separator 1 step back
- 1 2 3 4 5|6 7
- And we continue on moving first separator
- 1|2 3 4 5|6 7
- 1 2|3 4 5|6 7
- 1 2 3|4 5|6 7
- . . .
Here's my solution, it checks all possible combinations of splitting given number in 3 parts and checks whether sum of first two components are equal the third one.
def correct_number(x):
str_nmbr = str(x)
for result_split in range(len(str_nmbr)-2):
part_3 = int(str_nmbr[-result_split-1:])
for components_split in range(len(str_nmbr)-2-result_split):
part_2 = int(str_nmbr[1+components_split: -result_split-1])
part_1 = int(str_nmbr[:components_split+1])
if part_1 + part_2 == part_3:
return True
return False
print(correct_number(9999198)) # True
As author requested here's visual explanation how determining parts for number works, given number "1234567"
1 2 3 4 5 6 7:
- First loop chooses second separator
- 1 2 3 4 5 6|7
- Second loop chooses first one
- 1 2|3 4 5 6|7
- 1 2 3|4 5 6|7
- 1 2 3 4|5 6|7
- 1 2 3 4 5|6|7
- . . . Then we move second second separator 1 step back
- 1 2 3 4 5|6 7
- And we continue on moving first separator
- 1|2 3 4 5|6 7
- 1 2|3 4 5|6 7
- 1 2 3|4 5|6 7
- . . .
edited Nov 10 at 17:43
answered Nov 10 at 17:18
Filip Młynarski
40717
40717
@Looioe Thanx for the sol. can u explain the little bit how to print all possible 3 number combination? Like 7 into 2,2,3 and 1,3,3 and so on . I guess you use the same approach.
– user7390332
Nov 10 at 17:23
Also FYI What is a help vampire?
– jonrsharpe
Nov 10 at 17:25
@user7390332 first loop determines place where we split number into components side and result side for example with number of 7 digits we'd have to split it from 2 to the end (2 because we need at least 2 digits for first components). Second loop finds separator for components, we want it to be from 1 to place where starts our result number - 1. It would look like this: 1 2 3 4 5 6 7: First loop chooses second separator 1|2 3 4 5 6|7 1 2|3 4 5 6|7 1 2 3|4 5 6|7 . . .
– Filip Młynarski
Nov 10 at 17:32
add a comment |
@Looioe Thanx for the sol. can u explain the little bit how to print all possible 3 number combination? Like 7 into 2,2,3 and 1,3,3 and so on . I guess you use the same approach.
– user7390332
Nov 10 at 17:23
Also FYI What is a help vampire?
– jonrsharpe
Nov 10 at 17:25
@user7390332 first loop determines place where we split number into components side and result side for example with number of 7 digits we'd have to split it from 2 to the end (2 because we need at least 2 digits for first components). Second loop finds separator for components, we want it to be from 1 to place where starts our result number - 1. It would look like this: 1 2 3 4 5 6 7: First loop chooses second separator 1|2 3 4 5 6|7 1 2|3 4 5 6|7 1 2 3|4 5 6|7 . . .
– Filip Młynarski
Nov 10 at 17:32
@Looioe Thanx for the sol. can u explain the little bit how to print all possible 3 number combination? Like 7 into 2,2,3 and 1,3,3 and so on . I guess you use the same approach.
– user7390332
Nov 10 at 17:23
@Looioe Thanx for the sol. can u explain the little bit how to print all possible 3 number combination? Like 7 into 2,2,3 and 1,3,3 and so on . I guess you use the same approach.
– user7390332
Nov 10 at 17:23
Also FYI What is a help vampire?
– jonrsharpe
Nov 10 at 17:25
Also FYI What is a help vampire?
– jonrsharpe
Nov 10 at 17:25
@user7390332 first loop determines place where we split number into components side and result side for example with number of 7 digits we'd have to split it from 2 to the end (2 because we need at least 2 digits for first components). Second loop finds separator for components, we want it to be from 1 to place where starts our result number - 1. It would look like this: 1 2 3 4 5 6 7: First loop chooses second separator 1|2 3 4 5 6|7 1 2|3 4 5 6|7 1 2 3|4 5 6|7 . . .
– Filip Młynarski
Nov 10 at 17:32
@user7390332 first loop determines place where we split number into components side and result side for example with number of 7 digits we'd have to split it from 2 to the end (2 because we need at least 2 digits for first components). Second loop finds separator for components, we want it to be from 1 to place where starts our result number - 1. It would look like this: 1 2 3 4 5 6 7: First loop chooses second separator 1|2 3 4 5 6|7 1 2|3 4 5 6|7 1 2 3|4 5 6|7 . . .
– Filip Młynarski
Nov 10 at 17:32
add a comment |
3
Did you have a question? This isn't a code writing service.
– jonrsharpe
Nov 10 at 17:06
1
Did you try to do it? If yes please let us know where are you stuck?
– Sanchit Kumar
Nov 10 at 17:07
Sorry, yes I have a question. I am stuck on how to approach this problem.
– user7390332
Nov 10 at 17:08
That's not really a question, and certainly not on topic here. See How to Ask.
– jonrsharpe
Nov 10 at 17:11
@SanchitKumar , I am unable to come with an approach.
– user7390332
Nov 10 at 17:11