JavaScript Display number series with modulo in JavaScript display prime number series in python

I'm currently trying to display number series (for 2, 3, 4, 5, 6 and 7) in JavaScript. I was looking for the smallest number (x), which results in modulo = 1, if divided by 2, 3, 4, 5 and 6. If the same number (x) is divided by 7, id should result in modulo = 0. I'm not quite sure, if I'm explaining it correct. It should be like this: x % 2 = 1, x % 3 = 1, x % 4 = 1, x % 5 = 1, x % 6 = 1, x % 7 = 0.

The result is 301. My code looks like this and it works fine:

var seven = 7;
var six;
var five;
var four;
var three;
var two;

while (six != 1 || five != 1|| four != 1|| three != 1|| two != 1)
{six = seven % 6;
five = seven % 5;
four = seven % 4;
three = seven % 3;
two = seven % 2;
console.log(seven);
seven += 7;}

It displays all the numbers in the seven-series, until 301. Now, I wanted some more while-loops for the other numbers (2-6), that work the same why and show all the different numbers/steps in each series, until 301. I'm new to JavaScript and I just don't get it. I tried to modify my code, so that it should work with the other number series, but it doesn't. Or is there any other (maybe a better) way to do this? With some nested loops/functions? It only should be possible to display every number in each number series, but not all at the same time (in the end, there should be buttons, which show (by clicking them) the different number series with all the numbers/steps until 301). Thank you soso much!

Answer:1

When doing this you should probably use a loop to simplify your life.

Start x at 0 and iterate to (for example) 100.000. For every iteration, check to see if x % 2 / 3 / 4 / 5 / 6 is equal to 0. Then check to see if x % 7 === 1. If both these conditions are true, log the value and break the for loop.

The smallest value that answers this seems to be 120.

const numbers = [2,3,4,5,6]
const special = 7;

for(let x = 0; x < 100000; x++){
  const isModulo0ForAllNumbers = numbers.every(n => (x % n) === 0);
  const isModulo1ForSpecial = (x % special) === 1;
  
  if(isModulo0ForAllNumbers && isModulo1ForSpecial){
    console.log(`Smallest number found: ${x}`);
    break;
  }
}
Answer:2

Sometimes this is not possible to find such a number and you'll get infinite loop with unexpected behavior. This is a possible approach (see the comments inside):

// first define the greatest common divisor
// for two numbers - we'll need that later
let gcd = function(a, b) {
    // classic 'Euclidean' method with recursion
    if(a == 0) {
        return b;
    }
    if(a > b) {
        return gcd(b, a);
    }
    return gcd(b % a, a);
}

// define your series
let series = [2,3,4,5,6,7];

// now you need least common multiple for all numbers
// except for the last one
lcm = series[0];
for (let i = 1; i < series.length - 1; i++) {
    lcm *= series[i] / gcd(lcm, series[i])
}

// the last number from series
let last = series[series.length - 1];

// exercise: you can research or think for smarter solution
// I will just loop until we get the result
if(gcd(lcm, last) == 1) {
    let r = lcm + 1;
    while(r % last) {
        r += lcm;
    }

    console.log('number found: ', r);
} else {
    // not possible
    console.log('impossible to find the number');
}
Answer:3

I am working on a search function for a parent child structure with locations (city,suburb,street). I want to search for a street name and get the results but keeping the structure. The array ...

I am working on a search function for a parent child structure with locations (city,suburb,street). I want to search for a street name and get the results but keeping the structure. The array ...

  1. filter search multiple values excel
  2. filter search multiple values
  3. filter search multiple excel
  4. filter search multiple pivot
  5. search filter multiple
  6. search filter multiple php
  7. excel filter search multiple words
  8. django-filter search multiple fields
  9. angularjs filter search multiple fields
  10. angular filter search multiple fields
  11. excel filter search multiple criteria
  12. excel filter search multiple entries
  13. excel filter search multiple items
  14. excel filter search multiple columns
  15. angularjs search filter multiple parameters
  16. ldap search filter multiple ou
  17. angular search filter multiple parameters
  18. ldap search filter multiple groups
  19. ldap search filter multiple attributes
  20. ldap search filter multiple cn

It's silly, I know - every time I change environment I comment out and back my app's config factory. There must be a better way, is there? I though of checking the URL but this seems like a bad ...

It's silly, I know - every time I change environment I comment out and back my app's config factory. There must be a better way, is there? I though of checking the URL but this seems like a bad ...

When would I do <CustomComponent function = {this.FunctionName}/> instead of <CustomComponent function = {this.FunctionName.bind(this)}? React's documentation mentions that bind() solves ...

When would I do <CustomComponent function = {this.FunctionName}/> instead of <CustomComponent function = {this.FunctionName.bind(this)}? React's documentation mentions that bind() solves ...

  1. please simply explain

I have a small issue with a recursive .find() . when I use it to find ID === 2, it returns be the object with ID === 1 ... I have been scratching my head and I don't understand why that happens my ...

I have a small issue with a recursive .find() . when I use it to find ID === 2, it returns be the object with ID === 1 ... I have been scratching my head and I don't understand why that happens my ...