Skip to main content
edited tags
Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 309
Commonmark migration
Source Link

Given an array of positive or negative integers

 

I= [i1,..,in]

 

you have to produce a sorted array P of the form

 

[ [p, sum of all ij of I for which p is a prime factor (p positive) of ij] ...]

 

Example: I = [12, 15]; //result = [[2, 12], [3, 27], [5, 15]]

Given an array of positive or negative integers

 

I= [i1,..,in]

 

you have to produce a sorted array P of the form

 

[ [p, sum of all ij of I for which p is a prime factor (p positive) of ij] ...]

 

Example: I = [12, 15]; //result = [[2, 12], [3, 27], [5, 15]]

Given an array of positive or negative integers

I= [i1,..,in]

you have to produce a sorted array P of the form

[ [p, sum of all ij of I for which p is a prime factor (p positive) of ij] ...]

Example: I = [12, 15]; //result = [[2, 12], [3, 27], [5, 15]]

added 35 characters in body
Source Link
// Function that returns a collection of primes up to a given number
const sieveOfEratosthenes = (int) => {
  // Initialize some values
  const primes = [...Array(int + 1).keys()].slice(2);
  const sqrtCeil = Math.sqrt(int);
  let p = primes[0];
  let pIndex = 0;
  let factor = 2;

  // Until index refers to the last element, sieve primes
  while (pIndexprimes[pIndex] < primes.length - 1sqrtCeil) {
    const productIndex = primes.indexOf(factor * p);

    if (productIndex > -1) {
      primes.splice(productIndex, 1);
    }

    factor += 1;

    if (factor * p > primes[primes.length - 1]) {
      factor = 2;
      pIndex += 1;
      p = primes[pIndex];
    }
  }

  return primes;
};

function sumOfDivided(lst) {
  // If the input is empty, return an empty list
  if (lst.length === 0) {
    return [];
  }

  // Generate primes with a ceiling of the highest number in the input,
  // accounting for negative numbers
  const primes = sieveOfEratosthenes(
    Math.max(...lst.map((int) => (int < 0 ? int * -1 : int)))
  );

  // Reduce primes array to array of tuples, value one being being the prime,
  // value two being the sum of numbers it was a prime factor for in the input
  return primes.reduce((acc, prime) => {
    // Initialize values
    let sum = 0;
    let primeFactorCount = 0;

    // Check if each integer in the input had this prime as a factor
    for (let int of lst) {
      if (prime > int && int > 0) {
        continue;
      }

      if (int % prime === 0) {
        sum += int;
        primeFactorCount += 1;
      }
    }

    // If at least one number matched, add the tuple to the array
    if (primeFactorCount > 0) {
      return [...acc, [prime, sum]];
    }

    return acc;
  }, []);
}
```
// Function that returns a collection of primes up to a given number
const sieveOfEratosthenes = (int) => {
  // Initialize some values
  const primes = [...Array(int + 1).keys()].slice(2);
  let p = primes[0];
  let pIndex = 0;
  let factor = 2;

  // Until index refers to the last element, sieve primes
  while (pIndex < primes.length - 1) {
    const productIndex = primes.indexOf(factor * p);

    if (productIndex > -1) {
      primes.splice(productIndex, 1);
    }

    factor += 1;

    if (factor * p > primes[primes.length - 1]) {
      factor = 2;
      pIndex += 1;
      p = primes[pIndex];
    }
  }

  return primes;
};

function sumOfDivided(lst) {
  // If the input is empty, return an empty list
  if (lst.length === 0) {
    return [];
  }

  // Generate primes with a ceiling of the highest number in the input,
  // accounting for negative numbers
  const primes = sieveOfEratosthenes(
    Math.max(...lst.map((int) => (int < 0 ? int * -1 : int)))
  );

  // Reduce primes array to array of tuples, value one being being the prime,
  // value two being the sum of numbers it was a prime factor for in the input
  return primes.reduce((acc, prime) => {
    // Initialize values
    let sum = 0;
    let primeFactorCount = 0;

    // Check if each integer in the input had this prime as a factor
    for (let int of lst) {
      if (prime > int && int > 0) {
        continue;
      }

      if (int % prime === 0) {
        sum += int;
        primeFactorCount += 1;
      }
    }

    // If at least one number matched, add the tuple to the array
    if (primeFactorCount > 0) {
      return [...acc, [prime, sum]];
    }

    return acc;
  }, []);
}
```
// Function that returns a collection of primes up to a given number
const sieveOfEratosthenes = (int) => {
  // Initialize some values
  const primes = [...Array(int + 1).keys()].slice(2);
  const sqrtCeil = Math.sqrt(int);
  let p = primes[0];
  let pIndex = 0;
  let factor = 2;

  // Until index refers to the last element, sieve primes
  while (primes[pIndex] < sqrtCeil) {
    const productIndex = primes.indexOf(factor * p);

    if (productIndex > -1) {
      primes.splice(productIndex, 1);
    }

    factor += 1;

    if (factor * p > primes[primes.length - 1]) {
      factor = 2;
      pIndex += 1;
      p = primes[pIndex];
    }
  }

  return primes;
};

function sumOfDivided(lst) {
  // If the input is empty, return an empty list
  if (lst.length === 0) {
    return [];
  }

  // Generate primes with a ceiling of the highest number in the input,
  // accounting for negative numbers
  const primes = sieveOfEratosthenes(
    Math.max(...lst.map((int) => (int < 0 ? int * -1 : int)))
  );

  // Reduce primes array to array of tuples, value one being being the prime,
  // value two being the sum of numbers it was a prime factor for in the input
  return primes.reduce((acc, prime) => {
    // Initialize values
    let sum = 0;
    let primeFactorCount = 0;

    // Check if each integer in the input had this prime as a factor
    for (let int of lst) {
      if (prime > int && int > 0) {
        continue;
      }

      if (int % prime === 0) {
        sum += int;
        primeFactorCount += 1;
      }
    }

    // If at least one number matched, add the tuple to the array
    if (primeFactorCount > 0) {
      return [...acc, [prime, sum]];
    }

    return acc;
  }, []);
}
```
Source Link
Loading