Алгоритмы (Javascript)

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску

Код программ

Сумма вводимых целых чисел

1 let count = prompt('Введите число слагаемых')
2 
3 let sum = 0
4 for (let i=0; i < count; i++) {
5     let x = parseInt(prompt('Введите число ' + (i+1).toString()))
6     sum += x
7 }
8 
9 console.log('sum = ' + sum)

Произведение целых чисел

1 let count = prompt('Введите число множителей')
2 
3 let product = 1
4 for (let i=0; i < count; i++) {
5     let x = parseInt(prompt('Введите число ' + (i+1).toString()))
6     product *= x
7 }
8 
9 console.log('product = ' + product)

Сколько нечетных среди n введенных

 1 let count = prompt('Введите количество чисел')
 2 
 3 let oddCount = 0
 4 for (let i=0; i < count; i++) {
 5     let x = parseInt(prompt('Введите число ' + (i+1).toString()))
 6     if (x % 2 === 1) {
 7         oddCount++
 8     }
 9 }
10 
11 console.log('oddCount = ' + oddCount)

Защита от неверного ввода

1 let x = prompt('Введите x>0')
2 
3 while (x <= 0) {
4     alert("Неверный ввод")
5     x = prompt('Введите x>0')
6 }
7 
8 alert("x = " + x)

Вывод 10 первых степеней двойки

1 let x = 2
2 let n = 10
3 
4 for (let i = 0; i < n; i++) {
5     console.log("2 ** " + (i+1) + " = " + x)
6     x *= 2
7 }

Вывод всех двухзначных чисел, кратных 5

1 let x = 10
2 
3 while (x < 100) {
4     console.log(x)
5     x += 5
6 }

Вывод n первых чисел Фибоначчи

 1 let n = prompt('Введите число n')
 2 
 3 let a = 0
 4 let b = 1
 5 console.log(a)
 6 console.log(b)
 7 for(let i = 0; i < n-2; i++) {
 8     let tmp = a
 9     a = b
10     b = tmp + b
11     console.log(b)
12 }

Найти НОД(A,B), используя алгоритм Евклида:

 1 let a = parseInt(prompt('Введите число a'))
 2 let b = parseInt(prompt('Введите число b'))
 3 
 4 while (b !== 0) {
 5     let tmp = a
 6     a = b
 7     b = tmp % b
 8 }
 9 
10 console.log('НОД(a,b) = ' + a)

Найти сумму цифр целого числа m

 1 let m = parseInt(prompt('Введите число m'))
 2 
 3 let n = m
 4 let sum = 0
 5 while (n !== 0) {
 6     sum += n % 10
 7     n = Math.floor(n / 10)
 8 }
 9 
10 console.log('Сумма цифр числа ' + m + ' = ' + sum)

Найти max из введенных чисел

 1 let count = prompt('Введите количество чисел')
 2 
 3 let max = 0
 4 for (let i=0; i < count; i++) {
 5     let x = parseInt(prompt('Введите число ' + (i+1).toString()))
 6     if (i === 0) {
 7         max = x
 8     } else if (x > max) {
 9         max = x
10     }
11 }
12 
13 console.log('max = ' + max)

Найти min, удовлетворяющее условию p(x)

 1 function p(x) {
 2     return x > 0
 3 }
 4 
 5 let count = prompt('Введите количество чисел')
 6 let min = 0
 7 let found = false
 8 
 9 for (let i=0; i < count; i++) {
10     let x = parseInt(prompt('Введите число ' + (i+1).toString()))
11     if (found === false && p(x)) {
12         min = x
13         found = true
14     } else if (x < min && p(x)) {
15         min = x
16     }
17 }
18 
19 console.log('min & p(x) = ' + min)

Есть ли среди введенных число k?

 1 let count = prompt('Введите количество чисел')
 2 let needle  = prompt('Введите число для поиска')
 3 
 4 let found = false
 5 
 6 for (let i=0; i < count; i++) {
 7     let x = prompt('Введите число ' + (i+1).toString())
 8     if (x === needle) {
 9         found = true
10         break
11     }
12 }
13 
14 console.log(found ?
15     "Число " + needle + " было введено." :
16     "Число " + needle + " не было введено.")

Является ли число N>1 простым?

 1 let n = parseInt(prompt('Введите число n'))
 2 
 3 let isPrime = true
 4 for (let i = 2; i <= Math.floor(Math.sqrt(n)); i++) {
 5     console.log('i = ' + i)
 6     if (n % i === 0) {
 7         isPrime = false
 8         break
 9     }
10 }
11 
12 console.log(isPrime ?
13     "Число " + n + " простое." :
14     "Число " + n + " составное.")

Разложение числа на простые множители

 1 let x = parseInt(prompt('Введите число x'))
 2 
 3 let i = 2
 4 let res = x + ' = 1'
 5 
 6 while (x !== 1) {
 7     if (x % i === 0) {
 8         res += ' * ' + i
 9         x = Math.floor(x / i)
10     } else {
11         i++
12     }
13 }
14 
15 console.log(res)