Numbers with equal rises and falls: Difference between revisions
Content added Content deleted
(Ada version) |
m (No need for Big_Integers. Big speed improvement!) |
||
Line 253: | Line 253: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<lang Ada>with Ada.Text_Io; |
<lang Ada>with Ada.Text_Io; |
||
with Ada. |
with Ada.Integer_Text_Io; |
||
procedure Equal_Rise_Fall is |
procedure Equal_Rise_Fall is |
||
use Ada.Numerics.Big_Numbers.Big_Integers; |
|||
use Ada.Text_Io; |
use Ada.Text_Io; |
||
function Has_Equal_Rise_Fall (Value : |
function Has_Equal_Rise_Fall (Value : Natural) return Boolean is |
||
Rises : Natural := 0; |
Rises : Natural := 0; |
||
Falls : Natural := 0; |
Falls : Natural := 0; |
||
Image : constant String := |
Image : constant String := Natural'Image (Value); |
||
Last : Character := Image (Image'First + 1); |
Last : Character := Image (Image'First + 1); |
||
begin |
begin |
||
for Pos in Image'First + 2 .. Image'Last loop |
for Pos in Image'First + 2 .. Image'Last loop |
||
if Image (Pos) > Last then |
if Image (Pos) > Last then |
||
Rises := |
Rises := Rises + 1; |
||
elsif Image (Pos) < Last then |
elsif Image (Pos) < Last then |
||
Falls := |
Falls := Falls + 1; |
||
end if; |
end if; |
||
Last := Image (Pos); |
Last := Image (Pos); |
||
Line 276: | Line 275: | ||
end Has_Equal_Rise_Fall; |
end Has_Equal_Rise_Fall; |
||
Value |
Value : Natural := 1; |
||
Count |
Count : Natural := 0; |
||
begin |
begin |
||
loop |
loop |
||
if Has_Equal_Rise_Fall (Value) then |
if Has_Equal_Rise_Fall (Value) then |
||
Count := |
Count := Count + 1; |
||
if Count <= 200 then |
if Count <= 200 then |
||
Put |
Ada.Integer_Text_Io.Put (Value, Width => 5); |
||
if Count mod 20 = 0 then |
if Count mod 20 = 0 then |
||
New_Line; |
New_Line; |
||
Line 290: | Line 289: | ||
if Count = 10_000_000 then |
if Count = 10_000_000 then |
||
New_Line; |
New_Line; |
||
Put_Line ("The 10_000_000th: " & |
Put_Line ("The 10_000_000th: " & Natural'Image (Value)); |
||
exit; |
exit; |
||
end if; |
end if; |
||
end if; |
end if; |
||
Value := |
Value := Value + 1; |
||
end loop; |
end loop; |
||
end Equal_Rise_Fall;</lang> |
end Equal_Rise_Fall;</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
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 10_000_000th: 41909002 |
The 10_000_000th: 41909002</pre> |
||
</pre> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |