Advertisement
Farzona

Untitled

Feb 29th, 2020
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. //1. Найти сумму от одного до 100;
  2. console.log("№1");
  3. {
  4. let a = 100, sum = 0;
  5. for(let i = 0, j = 10; i <= 100; i++){
  6. sum = sum + i;
  7. // console.log(j);
  8. }
  9. console.log(sum);
  10. }
  11.  
  12. //2. Дано a -> (число а это n-значное число) найдите сумму цифр.
  13. {
  14. console.log("№2");
  15. let a = 123456, s = 0, i = 1, n = a;
  16. for(i = 1; i < n; i++){
  17. s += a % 10;
  18. a = (a - a%10) / 10;
  19. }
  20. console.log("S = " + s );
  21. }
  22.  
  23. //3. Выведите все четные числа от 10 до 50
  24. {
  25. console.log("№3 ");
  26. let a = 10, b = 50;
  27. for( let i = a; i <= b; i = i + 2){
  28. console.log(i);
  29. }
  30. }
  31.  
  32. //4. Выведите число 1 1-раза. 2 2-раз. 3 3-раза .. a a-раз,.
  33. {
  34. console.log("№4 ");
  35. let n = 10, j = 1;
  36. for(let i = 1; i <= 10; i++){
  37. for(j = 1; j <= i; j++ ){
  38. console.log(i);
  39. }
  40. j = 1;
  41. }
  42. }
  43.  
  44. //5. Дано n = 15, найдите число Фибоначи под n.Fin1 = 1 Fin2 = 1 Fin3 = Fin1 + Fin2; Fin(n) = Fin(n - 1) + Fin(n - 2);
  45. {
  46. console.log("№5");
  47. let n = 15, f1 = 1, f2 = 1, fn;
  48. for(let i = 3; i <= n; i++) {
  49. fn = f1 + f2;
  50. f1 = f2;
  51. f2 = fn;
  52. }
  53. console.log(fn);
  54. }
  55.  
  56. //6. Дано n = 5. найдите n!
  57. {
  58. console.log("№6");
  59. let n = 5, x = 1;
  60. for(let i = 1; i <= n; i++) {
  61. x = i * x;
  62. }
  63. console.log(x);
  64. }
  65.  
  66. //7. Дано n = 4, r = 2. Найдите Combination. Combination formula ( n! / ( r! * ( n -r )! ) )
  67. {
  68. console.log("№7");
  69. let i; s = 1; r = 2, n = 4;
  70. let x1 = 1, x2 = 1, x3 = 1;
  71. for(i = 1; i <= n; i++){
  72. x1 = x1 * i;
  73. }
  74. for(i = 1; i <= r; i++){
  75. x2 = x2 * i;
  76. }
  77. for(i = 1 ; i <= (n-r); i++){
  78. x3 = x3 * i;
  79. }
  80. s = x1 / (x2 * x3);
  81. console.log(s);
  82. }
  83.  
  84. //8. Дано число a. Простое ли число?
  85. {
  86. console.log("№8");
  87. let a = 10 , i = 2, k;
  88. let x = 0 ;
  89. if (a % 2 == 0 && a != 2) {
  90. console.log(" Нет ");
  91. }
  92. else {
  93. for( i = 2; i <= a; i++){
  94. if (a != i){
  95. k = a % i;
  96. if (k == 0){
  97. x = 1;
  98. }
  99. }
  100. }
  101.  
  102. if (x > 0){
  103. console.log("Нет");
  104. }
  105. if (x === 0){
  106. console.log("Да");
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement