Write a python function to find and display the six digit number in which the first digit is two more than the second, the second digit is two more than the third, the fourth digit is three greater than the third, the fifth digit is two more than the fourth and the last digit is the sum of fourth and fifth . The sum of all the digits is 22.
Answers
Answered by
1
The desired python code is given below-
from pylab import*
from numpy import*
def f():
for i in range(100000,1000000,1):
d1=i//100000
d2=(i%100000)//10000
d3=(i%10000)//1000
d4=(i%1000)//100
d5=(i%100)//10
d6=i%10
if d1==d2 +2 and d2==d3 +2 and d4==d3 +3 and d5==d4+2 and d6==d4+d5 and d1+d2+d3+d4+d5+d6==22:
x=i
return(x)
- The program is based on a for loop which runs through all the six digit numbers.
- d1,d2,d3,d4,d5,d6 are the first ,second ,third , fourth ,fifth and sixth digits of the six digit number respectively that is being stored in the variable i.
- The digits of the number is calculated mathematically by making use of the floor divide and the modulus operators.
- All this is followed by an if statement which allows the six digit number to be stored in x only if all the conditions of the question are satisfied.
- this python code runs to return the output of 420358 as the desired six digit number if the function f is called by using 'f()'.
Similar questions
Math,
6 months ago
English,
6 months ago
Computer Science,
1 year ago
Social Sciences,
1 year ago
Chemistry,
1 year ago