-
Notifications
You must be signed in to change notification settings - Fork 0
/
0000.js
167 lines (141 loc) · 3.66 KB
/
0000.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const isPrime = function (n) {
if (n == 1) return false;
else if (n < 4) return true; //2 and 3 are prime
else if (n % 2 == 0) return false;
else if (n < 9) return true; //we have already excluded 4,6 and 8.
else if (n % 3 == 0) return false;
else var r = Math.floor(Math.sqrt(n)); // n rounded to the greatest integer r so that r*r<=n
var f = 5;
while (f <= r) {
if (n % f == 0) return false;
if (n % (f + 2) == 0) return false;
f = f + 6;
}
return true;
};
const fibonacci = function (num) {
if (num < 2) {
return num;
} else {
return fibonacci(num - 1) + fibonacci(num - 2);
}
};
const isPrimeRMG = function (num) {
if (num === 2) {
return true;
} else if (num % 2 === 0) {
return false;
} else {
let encontrouDivisor = false;
for (let i = 2; i < num / 2; i++) {
if (num % i === 0) {
encontrouDivisor = true;
}
}
return !encontrouDivisor;
}
};
const isPalindrome = function (valor) {
const original = valor;
valor = valor + '';
valor = valor.split('');
let contrario = '';
for (let i = valor.length - 1; i >= 0; i--) {
contrario += valor[i];
}
return original == contrario;
};
function decomporNumero(numero) {
let restoParaDecompor = numero;
if (isPrime(numero)) {
return [1, numero];
}
let divisores = [];
let n = 2;
while (restoParaDecompor > 1) {
if (isPrime(n)) {
while (restoParaDecompor % n == 0) {
restoParaDecompor = restoParaDecompor / n;
divisores.push(n);
}
}
n++;
}
return divisores;
}
function divisoresDeUmNumero(numero) {
let divisores = [];
divisores.push(1);
for (let i = 2; i <= numero / 2; i++) {
if (numero % i == 0) {
divisores.push(i);
}
}
divisores.push(numero);
return divisores;
}
function factorial(num) {
if (num <= 1) {
return 1;
}
return num * factorial(num - 1);
}
function quantidadeDeVezesNumeroPossuiDigito(numero, digito) {
let digitosDoNumero = ('' + numero).split('');
let quantidadeDeVezes = 0;
digitosDoNumero.forEach((n) => {
if (n == digito) {
quantidadeDeVezes++;
}
});
return quantidadeDeVezes;
}
// função mais simples para verificar se um número é pandigital
export function isPandigital(numero) {
const sortedIdentity = numero.toString().split('').sort().join('');
return sortedIdentity === '123456789';
}
function verificaSeNumeroPandigital(numero) {
const checaNumero0 = quantidadeDeVezesNumeroPossuiDigito(numero, 0);
if (checaNumero0 > 0) {
return false;
}
const checaNumero1 = quantidadeDeVezesNumeroPossuiDigito(numero, 1);
const checaNumero2 = quantidadeDeVezesNumeroPossuiDigito(numero, 2);
const checaNumero3 = quantidadeDeVezesNumeroPossuiDigito(numero, 3);
const checaNumero4 = quantidadeDeVezesNumeroPossuiDigito(numero, 4);
const checaNumero5 = quantidadeDeVezesNumeroPossuiDigito(numero, 5);
const checaNumero6 = quantidadeDeVezesNumeroPossuiDigito(numero, 6);
const checaNumero7 = quantidadeDeVezesNumeroPossuiDigito(numero, 7);
const checaNumero8 = quantidadeDeVezesNumeroPossuiDigito(numero, 8);
const checaNumero9 = quantidadeDeVezesNumeroPossuiDigito(numero, 9);
if (
checaNumero1 == 1 &&
checaNumero2 == 1 &&
checaNumero3 == 1 &&
checaNumero4 == 1 &&
checaNumero5 == 1 &&
checaNumero6 == 1 &&
checaNumero7 == 1 &&
checaNumero8 == 1 &&
checaNumero9 == 1
) {
return true;
} else {
return false;
}
}
export {
/*
Calcula a sequência de fibonacci
*/
fibonacci,
isPrime,
isPrimeRMG,
isPalindrome,
divisoresDeUmNumero,
decomporNumero,
factorial,
quantidadeDeVezesNumeroPossuiDigito,
verificaSeNumeroPandigital,
};