Skip to content

Commit

Permalink
Solução do desafio 11.
Browse files Browse the repository at this point in the history
  • Loading branch information
WhoisBsa committed Sep 23, 2023
1 parent a22ddbe commit 11bda7e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions desafio-11/WhoisBsa/javascript/.valid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
141c8a1f3035efa05ebd1bee90e29a26c
70 changes: 70 additions & 0 deletions desafio-11/WhoisBsa/javascript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Primes in pi - WhoisBsa
import fs from 'fs';

const data = fs.readFileSync('./data.txt', 'utf8');
const numbers = data.slice(2);
const primeList = new Set<number>();

let primeSequence = '';
let maxSequence = '';
let currentSequence = '';
let currentNumber = '';
let numberIndex = 0;

const minPrime = 2;
const maxPrime = 9973;
const isPrimeNumber = (number: number) => {
if (number < 2 || (number % 2 == 0 && number > 2)) return false;

const s = Math.sqrt(number);
for (let i = 3; i <= s; i += 2)
if (number % i === 0) return false;

primeList.add(number);
};

isPrimeNumber(minPrime);
for (var i = minPrime - 1; i <= maxPrime; i += 2)
isPrimeNumber(i);

for (let i = 1; i < numbers.length; i++) {
if (primeSequence.length > numbers.length - i) break;

while (numberIndex < numbers.length)
getPrimeSequence(numberIndex, 5);

if (maxSequence.length > primeSequence.length)
primeSequence = maxSequence;

maxSequence = '';
numberIndex = i;
}

function getPrimeSequence(currentPosition: number, slice: number): void {
if (slice === 0) {
if (!currentSequence) {
numberIndex = numbers.length;
return;
}

return addToSequence();
}

currentNumber = numbers.slice(currentPosition, currentPosition + slice);

if (primeList.has(+currentNumber)) {
currentSequence = currentNumber;

if (currentNumber.length === 2)
return addToSequence();
}
getPrimeSequence(currentPosition, slice - 1);
}

function addToSequence(): void {
numberIndex += currentSequence.length;
maxSequence += currentSequence;
currentSequence = '';
}

console.log({ primeSequence });

0 comments on commit 11bda7e

Please sign in to comment.