-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
executable file
·183 lines (155 loc) · 6.21 KB
/
test.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//============================ Problem 1: Multiple of three and five ====================================
/**
* Description: If we list all the natural numbers below 10 that are multiple of 3 and 5, we get 3, 5, 6 and 9.
* the sum of these multiplies is 23.
* Find the sum of all the multiples of 3 and 5 below 1000.
*/
// let total = 0
// for( let i = 1; i < 1000; i++){
// if(i%3===0 || i%5 === 0){
// total += i
// }
// }
// console.log('total', total)
//============================= Problem 2: Even Fibonacci numbers ========================================================================================
/**
* Description: Each new term of fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first
* 10 terms will be:
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.......
* By considering the terms in the Fibonnacci sequence whose value do not exceed four million, find the sum
* of the even valued terms.
*
*/
// let total = 0, prev = 1, next = 2
// while((total + prev) < 1000000){
// if(prev % 2 === 0){
// total = total + prev
// }
// const fabSerise = prev
// console.log(prev)
// prev = next
// next = fabSerise + next
// }
// console.log('total', total)
//============================= Problem 3: Largest Prime Factor ========================================================================================
/**
* Description: the prime factor of 13195 qre 5, 7, 13 and 29.
* what is the prime factor of the number 600851475143
*/
// let largestPrimeFactor = 1, number = 600851475143
// for( let i =2; i<number; i++){
// while(number % i === 0 ){
// number = number / i
// if(i > largestPrimeFactor ){
// largestPrimeFactor = i
// }
// }
// }
// console.log('largestPrimeFactor', largestPrimeFactor)
//============================= Problem 4: Largest palindrome product ========================================================================================
/**
* Description: A palindromic number reads the same both ways. the largest palindrome made from the product of
* two digit number is 9009 = 91 x 99
* Find the largest palindrome made from the product of two 3-digit numbers.
*/
//============================= Problem 5: Smallest multiple ========================================================================================
/**
* 2520 is the smallest number that can be devided by each of number from 1 to 10
* What is the smallest positive number that is evenly devisible by all the numbers from 1 to 20
*/
// let smallestMultiple = 1
// for( let i = 10; i>1 ; i--){
// if(smallestMultiple%i){
// console.log('i', i)
// smallestMultiple = smallestMultiple * i
// }
// }
// console.log('smallestMultiple', smallestMultiple)
//============================= Problem 6: Sum square diffrence ========================================================================================
/**
* Description: The sum of the squares of the first ten natural numbers is
* 1^2+2^2+3^2+4^2+5^2 ..... 10^2 = 385
* The square of the sum of the first ten natural numbers is,
* (1+2+3+4+5.......10)^2 = 55^2 = 3025
* Hence the diffrence between the sum of the squares of the first ten natural numbers and the square
* of the sum is 3025-385 = 2640
* Find the diffrence between the sum of the squares of the first one hundred natural numbers and the square of the sum
*/
// let n = 10, sumOfSquare, sum, squareOfSum, diffrence
// sumOfSquare = n*(n+1)*(2*n+1)/6
// sumOfNnumbers = n*(n+1)/2
// squareOfSum = sumOfNnumbers * sumOfNnumbers
// diffrence = sumOfSquare - squareOfSum
// console.log('diffrence', diffrence)
//============================= Problem 7: 10001st prime ========================================================================================
/**
* By listing the first six prime numbers: 2,3,5,7,11, and 13, we can see that the 6th prime number is 13
* What is the 10001st prime number?
*/
// let n = [], i=1
// console.time('time')
// const isPrimeNumber = (number) => {
// if(number===1){
// return false
// }else if(number === 2){
// return true
// }else{
// for(let j=2; j<number; j++ ){
// if(number%j === 0){
// return false
// }
// }
// return true
// }
// }
// while(n.length< 10001){
// const isPrime = isPrimeNumber(i)
// if(isPrime){
// n.push(i)
// }
// i++
// }
// console.timeEnd('time')
// console.log('10 prime numbers', n[n.length-1])
//============================= Problem 8: Largest product in a series ========================================================================================
/**
* Description: The four adjecent digits in the 1000- digit number that have graetest product are 9x9x8x9=5832
* Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. what is the value
* of this product?
*/
//============================= Problem 9: Special Pythagorean triplet ========================================================================================
/**
* Description: A Pythagorean triplet is a set of three natural numbers, a<b<c, for which,
* a^2+b^2=c^2
* For example, 3^2+4^2= 5^2
* There exists exactly one Pythagorean triplet for which a+b+c=1000
* Find the product abc
*/
//============================= Problem 10: Summation of primes ========================================================================================
/**
* The sum of prime below 10is 2+3+5+7 = 17
* Find the sum of all the primes below two million
*/
let n = [], i = 1
const isPrimeNumber = (number) => {
if (number === 1) {
return false
} else if (number === 2) {
return true
} else {
for (let j = 2; j < number; j++) {
if (number % j === 0) {
return false
}
}
return true
}
}
while (n.length < 200) {
// const isPrime = isPrimeNumber(i)
// if (isPrime) {
// n.push(i)
// }
i++
}
console.log('Sum: ', n)