0

how to make loop to print every number that only contain 7 or 8 or both?

ex 7,8,77,78,87,88,777,778,787......

29th Nov 2018, 2:30 PM
Yoaraci
Yoaraci - avatar
3 Respostas
+ 6
I'd do it with a bit pattern. Zero becomes 7 and one becomes 8. The tricky part is getting the shorter numbers. Say you want 8 digit numbers. The range of 0 to 255 would provide all 8 digit combinations of 7 & 8. To get the shorter numbers, you must detect the upper bits are zero so you could also generate them. The range of 0 to 1 should also output single digit numbers. The range of 0 to 3 should output double digits, etcetera.
30th Nov 2018, 1:10 AM
John Wells
John Wells - avatar
+ 3
I tried a recursive approach. A function takes a number, then creates two new numbers by appending a trailing 7 and 8. Then calls itself again with these new numbers. The function also receives the desired number of trailing digits. Then decrements it as it appends new digits. When number of trailing digits reaches zero, the function outputs the resulting numbers. https://code.sololearn.com/cul4c36c88As/?ref=app The input is the maximum digit count. The code only accepts a single digit number - lengths above 9 cause the parser to timeout.
30th Nov 2018, 2:31 AM
Emerson Prado
Emerson Prado - avatar
2nd Dec 2018, 12:06 AM
Emerson Prado
Emerson Prado - avatar