Numbers with equal rises and falls: Difference between revisions
Content added Content deleted
m (No need for Big_Integers. Big speed improvement!) |
(Added C++ and Swift solutions) |
||
Line 517: | Line 517: | ||
The 10,000,000th number is: 41909002</pre> |
The 10,000,000th number is: 41909002</pre> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <iomanip> |
|||
#include <iostream> |
|||
bool equal_rises_and_falls(int n) { |
|||
int total = 0; |
|||
for (int previous_digit = -1; n > 0; n /= 10) { |
|||
int digit = n % 10; |
|||
if (previous_digit > digit) |
|||
++total; |
|||
else if (previous_digit >= 0 && previous_digit < digit) |
|||
--total; |
|||
previous_digit = digit; |
|||
} |
|||
return total == 0; |
|||
} |
|||
int main() { |
|||
const int limit1 = 200; |
|||
const int limit2 = 10000000; |
|||
int n = 0; |
|||
std::cout << "The first " << limit1 << " numbers in the sequence are:\n"; |
|||
for (int count = 0; count < limit2; ) { |
|||
if (equal_rises_and_falls(++n)) { |
|||
++count; |
|||
if (count <= limit1) |
|||
std::cout << std::setw(3) << n << (count % 20 == 0 ? '\n' : ' '); |
|||
} |
|||
} |
|||
std::cout << "\nThe " << limit2 << "th number in the sequence is " << n << ".\n"; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
The first 200 numbers in the sequence are: |
|||
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102 |
|||
103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 |
|||
153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 |
|||
185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 |
|||
209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 |
|||
253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 |
|||
285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 |
|||
309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 |
|||
343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 |
|||
381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404 |
|||
The 10000000th number in the sequence is 41909002. |
|||
</pre> |
|||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
Line 1,513: | Line 1,562: | ||
<pre> |
<pre> |
||
the 10,000,000th number is: 41,909,002 |
the 10,000,000th number is: 41,909,002 |
||
</pre> |
|||
=={{header|Swift}}== |
|||
<lang swift>import Foundation |
|||
func equalRisesAndFalls(_ n: Int) -> Bool { |
|||
var total = 0 |
|||
var previousDigit = -1 |
|||
var m = n |
|||
while m > 0 { |
|||
let digit = m % 10 |
|||
m /= 10 |
|||
if previousDigit > digit { |
|||
total += 1 |
|||
} else if previousDigit >= 0 && previousDigit < digit { |
|||
total -= 1 |
|||
} |
|||
previousDigit = digit |
|||
} |
|||
return total == 0 |
|||
} |
|||
var count = 0 |
|||
var n = 0 |
|||
let limit1 = 200 |
|||
let limit2 = 10000000 |
|||
print("The first \(limit1) numbers in the sequence are:") |
|||
while count < limit2 { |
|||
n += 1 |
|||
if equalRisesAndFalls(n) { |
|||
count += 1 |
|||
if count <= limit1 { |
|||
print(String(format: "%3d", n), terminator: count % 20 == 0 ? "\n" : " ") |
|||
} |
|||
} |
|||
} |
|||
print("\nThe \(limit2)th number in the sequence is \(n).")</lang> |
|||
{{out}} |
|||
<pre> |
|||
The first 200 numbers in the sequence are: |
|||
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 102 |
|||
103 104 105 106 107 108 109 111 120 121 130 131 132 140 141 142 143 150 151 152 |
|||
153 154 160 161 162 163 164 165 170 171 172 173 174 175 176 180 181 182 183 184 |
|||
185 186 187 190 191 192 193 194 195 196 197 198 201 202 203 204 205 206 207 208 |
|||
209 212 213 214 215 216 217 218 219 222 230 231 232 240 241 242 243 250 251 252 |
|||
253 254 260 261 262 263 264 265 270 271 272 273 274 275 276 280 281 282 283 284 |
|||
285 286 287 290 291 292 293 294 295 296 297 298 301 302 303 304 305 306 307 308 |
|||
309 312 313 314 315 316 317 318 319 323 324 325 326 327 328 329 333 340 341 342 |
|||
343 350 351 352 353 354 360 361 362 363 364 365 370 371 372 373 374 375 376 380 |
|||
381 382 383 384 385 386 387 390 391 392 393 394 395 396 397 398 401 402 403 404 |
|||
The 10000000th number in the sequence is 41909002. |
|||
</pre> |
</pre> |
||