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......
4 odpowiedzi
+ 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.
+ 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.
+ 3
A web version, as suggested by https://www.sololearn.com/Profile/10042983/?ref=app (how do I mention him here?)
Pablo Escobar ll
https://code.sololearn.com/WGQrqYOMp4ne/?ref=app