Skip to content

Desafios de lógica de programação - Project Euler

Notifications You must be signed in to change notification settings

WesleyKaihara/ProjectEuler

Repository files navigation

Problem_1

// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
// Find the sum of all the multiples of 3 or 5 below 1000.

let s = 0
for(let i = 3; i< 1000; i++) {
  if(i%3 == 0 || i%5 == 0) {
    s+=i
  }
}

console.log(s)

Problem_2

// Each new term in the 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 Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

let k = 0
let a = 1;
let b = 1;

while(a+b < 4000000) {
  b+=a
  a=b-a
  if(b%2 == 0) k+=b
}

console.log(k)

Problem_3

// The prime factors of 13195 are 5, 7, 13 and 29.
// What is the largest prime factor of the number 600851475143 ?

let n = 600851475143
let d = 2

while(n != d) {
  if(n % d == 0) {
    n/=d
  } else {
    d++
  }
}

console.log(d)

Problem_4

// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
// Find the largest palindrome made from the product of two 3-digit numbers.

let res = 0

for(let i = 100; i < 999; i++) {
  for(let j = 999; j > 100; j--){
    let m = j*i
    if(m == m.toString().split('').reverse().toString().replace(/,/gi,"") && m > res) {
      res = m
    }
  }
}

console.log(res)

Problem_5

// 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
// What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

let res = 20;
for (let i = 20 - 1; i > 1; i--) {
  if (res % i !== 0) {
    res += 20;
    i = 20
  }
}
console.log(res);

Problem_6

// The sum of the squares of the first ten natural numbers is,

// The square of the sum of the first ten natural numbers is,

// Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is .

// Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

let s = 0
let q = 0

for(let i = 1; i <= 100; i++) {
    s+=Math.pow(i,2)
    q+=i
}

console.log(Math.pow(q,2) - s)

Problem_7

// By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
// What is the 10001st prime number?

function isPrime(n) {
  if (n <= 1) return
  for (let k = 2; k <= Math.sqrt(k); k++) {
    if (n % k === 0) return
  }
  return true
}

let count = 0
let i = 2
while (count < 10001) {
  if (isPrime(i)) {
    count++
  }
  i++
}
console.log(i-1)

Problem_8

// The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

// 73167176531330624919225119674426574742355349194934
// 96983520312774506326239578318016984801869478851843
// 85861560789112949495459501737958331952853208805511
// 12540698747158523863050715693290963295227443043557
// 66896648950445244523161731856403098711121722383113
// 62229893423380308135336276614282806444486645238749
// 30358907296290491560440772390713810515859307960866
// 70172427121883998797908792274921901699720888093776
// 65727333001053367881220235421809751254540594752243
// 52584907711670556013604839586446706324415722155397
// 53697817977846174064955149290862569321978468622482
// 83972241375657056057490261407972968652414535100474
// 82166370484403199890008895243450658541227588666881
// 16427171479924442928230863465674813919123162824586
// 17866458359124566529476545682848912883142607690042
// 24219022671055626321111109370544217506941658960408
// 07198403850962455444362981230987879927244284909188
// 84580156166097919133875499200524063689912560717606
// 05886116467109405077541002256983155200055935729725
// 71636269561882670428252483600823257530420752963450

// Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?


let v='731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357297271636269561882670428252483600823257530420752963450'

let m = 1
for(let i = 0; i < v.length - 12; i++) {
  let z = 1
  for(let j = 0; j <13; j++) {
    z*=parseInt(v[i+j])
  }
  if(z > m) {
    m = z
  }
}

console.log(m) 

Problem_9

// A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

// a2 + b2 = c2
// For example, 32 + 42 = 9 + 16 = 25 = 52.

// There exists exactly one Pythagorean triplet for which a + b + c = 1000.
// Find the product abc.

for(let a=1; a < 1000;a++) {
  for(let b = a; b < 1000; b++) {
    for(let c = b ; c < 1000; c++) {
      if(Math.pow(a,2) + Math.pow(b,2) == Math.pow(c,2)) {
        if(a+b+c == 1000 ){
          console.log(a*b*c)
          console.log(a,'+',b,'+',c,'=',a+b+c)
          break
        } 
      }
    }
  }
}

Problem_10

// The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
// Find the sum of all the primes below two million.

function isPrime(n) {
  if (n <= 1) return
  for (let k = 2; k <= Math.sqrt(n); k++) {
    if (n % k === 0) return
  }
  return true
}

let s = 0
for(let i = 2; i < 2000000; i++) {
  if(isPrime(i)) {
    s+=i
  }
}

console.log(s)

Problem_11

// In the 20×20 grid below, four numbers along a diagonal line have been marked in red.

// 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
// 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
// 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
// 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
// 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
// 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
// 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
// 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
// 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
// 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
// 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
// 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
// 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
// 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
// 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
// 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
// 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
// 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
// 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
// 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

// The product of these numbers is 26 × 63 × 78 × 14 = 1788696.

// What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?

Problem_12

// The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1+2+3+4+5+6+7=28.The first ten terms would be:
// 1,3,6,10,15,21,28,36,45,55...
// Let us list the factors of the first seven triangle numbers:

// 1: 1 
// 3: 1,3
// 6: 1,2,3,6
// 10: 1,2,5,10
// 15: 1,2,3,5,15
// 21: 1,3,7,21
// 28: 1,2,4,7,14,28 

// We can see that 28 is the first triangle number to have over five divisors.
// What is the value of the first triangle number to have over five hundred divisors?

let divisors_amount = 0
let s = 0;

for(let i=1; divisors_amount < 500; i++) {
  s=0
  for(let j=1; j<=i; j++) {
    s+=j
  }
  
  // first and last number
  divisors_amount=2
  let m=s-1

  for(let k=2; k<m; k++) {
    if(s % k == 0) {
      divisors_amount+=2
      m=s/k
    }
  }
}

console.log(s)

Problem_13

// Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

const fs = require('fs')
var s = BigInt(0)
fs.readFile(`${__dirname}/data.txt`, 'utf8' , (err, data) => {
  var l = data.split(/\r?\n/);
  l.forEach(function(k){
     s+=BigInt(k)
  })
  console.log(String(s).substr(0,10))
})

Problem_17

// If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

// If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

// NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.

const n = [0,3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8]
const d = [0,3,6,6,5,5,5,7,6,6]
let l = 1

for (let i = 1; i <= 1000; i++) {
  let cn = Math.trunc(i/100)
  let dz = Math.trunc((i-cn*100)/10)
  let und = i-cn*100-dz*10

  // 100 a 999
  if (i > 99) {
    l+=n[cn]+7
    if (i%100!==0) {
      l+=3
    }
  }

  // 20-99
  if (dz > 1) {
    l+=d[dz]
    if (und!== 0) {
      l+=n[und]
    }
  }

  // 11-19
  if (dz === 1) {
    l+=n[dz*10+und]
  }

  // 1-10
  if (dz===0 && und!== 0 || i===1000) {
    l += n[und]
  }
}

console.log(l)

Problem_18

// By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

// 3
// 7 4
// 2 4 6
// 8 5 9 3

// That is, 3 + 7 + 4 + 9 = 23.

// Find the maximum total from top to bottom of the triangle below:

// 75
// 95 64
// 17 47 82
// 18 35 87 10
// 20 04 82 47 65
// 19 01 23 75 03 34
// 88 02 77 73 07 63 67
// 99 65 04 28 06 16 70 92
// 41 41 26 56 83 40 80 70 33
// 41 48 72 33 47 32 37 16 94 29
// 53 71 44 65 25 43 91 52 97 51 14
// 70 11 33 28 77 73 17 78 39 68 17 57
// 91 71 52 38 17 14 91 43 58 50 27 29 48
// 63 66 04 68 89 53 67 30 73 16 69 87 40 31
// 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

// NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred r; it cannot be solved by brute force, and requires a clever method! ;o)

const fs = require('fs');

fs.readFile(`${__dirname}/data.txt`, 'utf8' , (err, data) => {
  
  const d = data.split('\n').map(l => l.split(' ').map(Number));

  for (let i = d.length -2; i >= 0; i--) {
    for (let j=0; j<d[i].length; j++) {
      d[i][j] += Math.max(d[i+1][j], d[i+1][j+1]);
    }
  }

  console.log(d[0][0]);
});

Problem_20

// n! means n × (n − 1) × ... × 3 × 2 × 1

// For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
// and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

// Find the sum of the digits in the number 100!

let i = BigInt(1)
let r = 0;

for(let n = BigInt(100);n > 0;n--) {
  i*=n;
}

for(n=0;n<String(i).length;n++) {
  r+=Number(String(i)[n])
}
console.log(r)

Problem_22

// Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

// For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

// What is the total of all the name scores in the file?

const fs = require('fs');

let letras = {
  A:1,B:2,C:3,D:4,E:5,F:6,G:7,H:8,I:9,J:10,K:11,L:12,M:13,N:14,O:15,P:16,Q:17,R:18,S:19,T:20,U:21,V:22,W:23,X:24,Y:25,Z:26
}

let total = 0

fs.readFile(`${__dirname}/data.txt`, 'utf8' , (err, data) => {
  const nomes = data.split(",").map(nome => 
    nome = nome.replace(/"/g,"")
  ).sort()

  nomes.forEach((nome,index) => {
    let score = 0
    for(let i=0; i<nome.length;i++) {
      score+=letras[nome[i]]
    }
    total+=score*(index+1)
  })
  console.log(total)
});

About

Desafios de lógica de programação - Project Euler

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published