-
Notifications
You must be signed in to change notification settings - Fork 118
/
divisible-numbers.js
94 lines (87 loc) · 4.17 KB
/
divisible-numbers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Write a program that iterates through integers from 0 to infinity and
// displays those integers that are divisible without remainder by the number
// of digits of this number (i.e., those two-digit numbers that are divisible by 2,
// three-digit numbers by three, etc.). The program should stop when N (specified by the user)
// of such numbers is output.
// Counts digits as the length of a string
const findDivisibleNumbersUsingString = n => {
if (n < 0) throw new Error('n must be non-negative.');
let count = 0;
let i = 0;
while (count !== n) {
if (i < 10 || !(i % `${i}`.length)) {
console.log(i);
count++;
}
i++;
}
};
findDivisibleNumbersUsingString(28);
// 0 1 2 3 4 5 6 7 8 9 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44
// Counts digits using math
const countDigits = n => {
let digits = 0;
while (n) {
n = Math.floor(n / 10);
digits++;
}
return digits;
};
const findDivisibleNumbersUsingMath = n => {
if (n < 0) throw new Error('n must be non-negative.');
let count = 0;
let i = 0;
while (count !== n) {
if (i < 10 || !(i % countDigits(i))) {
console.log(i);
count++;
}
i++;
}
};
findDivisibleNumbersUsingMath(120);
// 0 1 2 3 4 5 6 7 8 9 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
// 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147
// 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216 219 222 225 228 231
// 234 237 240 243 246 249 252 255 258 261 264 267 270 273 276 279 282 285 288 291 294
// More efficient version
const findDivisibleNumbersQuick = n => {
if (n < 0) throw new Error('n must be non-negative.');
let start = 0;
let end = 10;
let digits = 1;
let count = 0;
while (n !== count) {
for (let j = start; j < end; j += digits) {
console.log(j);
count++;
if (n === count) return;
}
digits++;
start = end;
end = start * 10;
while (start % digits) {
start++;
}
}
};
findDivisibleNumbersQuick(500);
// 0 1 2 3 4 5 6 7 8 9 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
// 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147
// 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216 219 222 225 228 231
// 234 237 240 243 246 249 252 255 258 261 264 267 270 273 276 279 282 285 288 291 294 297 300 303 306 309 312 315
// 318 321 324 327 330 333 336 339 342 345 348 351 354 357 360 363 366 369 372 375 378 381 384 387 390 393 396 399
// 402 405 408 411 414 417 420 423 426 429 432 435 438 441 444 447 450 453 456 459 462 465 468 471 474 477 480 483
// 486 489 492 495 498 501 504 507 510 513 516 519 522 525 528 531 534 537 540 543 546 549 552 555 558 561 564 567
// 570 573 576 579 582 585 588 591 594 597 600 603 606 609 612 615 618 621 624 627 630 633 636 639 642 645 648 651
// 654 657 660 663 666 669 672 675 678 681 684 687 690 693 696 699 702 705 708 711 714 717 720 723 726 729 732 735
// 738 741 744 747 750 753 756 759 762 765 768 771 774 777 780 783 786 789 792 795 798 801 804 807 810 813 816 819
// 822 825 828 831 834 837 840 843 846 849 852 855 858 861 864 867 870 873 876 879 882 885 888 891 894 897 900 903
// 906 909 912 915 918 921 924 927 930 933 936 939 942 945 948 951 954 957 960 963 966 969 972 975 978 981 984 987
// 990 993 996 999 1000 1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044 1048 1052 1056 1060 1064 1068 1072
// 1076 1080 1084 1088 1092 1096 1100 1104 1108 1112 1116 1120 1124 1128 1132 1136 1140 1144 1148 1152 1156 1160
// 1164 1168 1172 1176 1180 1184 1188 1192 1196 1200 1204 1208 1212 1216 1220 1224 1228 1232 1236 1240 1244 1248
// 1252 1256 1260 1264 1268 1272 1276 1280 1284 1288 1292 1296 1300 1304 1308 1312 1316 1320 1324 1328 1332 1336
// 1340 1344 1348 1352 1356 1360 1364 1368 1372 1376 1380 1384 1388 1392 1396 1400 1404 1408 1412 1416 1420 1424
// 1428 1432 1436 1440 1444 1448 1452 1456 1460 1464 1468 1472 1476 1480 1484 1488 1492 1496 1500 1504 1508 1512
// 1516 1520 1524 1528 1532 1536 1540 1544 1548 1552 1556 1560 1564 1568 1572 1576