Search code examples
javascriptalgorithmfunctionnumber-theory

Function to determine if a range of consecutive (positive) integers can be written as the sum of two (positive) composite-relatively prime integers


I would like to have a function that determines whether every integer n in a range of consecutive positive integers from a to b (inclusive) can be written as the sum of two positive composite integers both relatively prime to n.

The function input and output should be of the form:

  1. function(a,b) = FALSE, the highest integer in the range that failed the test is c
  2. function(a,b) = TRUE, all integers in the range passed the test

INPUT:

For the input, the user just types two integers where 1 ≤ a < b.

  • If the user types an invalid integer, there should be an error message like "invalid choice of integers".
  • Something similar must happen if there's at least one missing value for the input.

OUTPUT:

The output of the function should start with the Boolean value FALSE if there's at least one integer n, in the range from a to b, that cannot be written as the sum of two (positive) composite integers both relatively prime to n.

The function should then output the highest of such integers that fails the test. The full output should be printed in the console using format (1) above: Boolean value FALSE + string of characters (as shown) + highest integer that failed the test.

On the other hand, the output of the function should start with the Boolean value TRUE if each integer n in the range of integers from a to b can be written as the sum of two (positive) composite integers both relatively prime to n. The full output should be printed in the console using format (2) above: Boolean value TRUE + string of characters (as shown).

Example:

Consider the range of integers (90,100) - that is, integers starting from 90 until 100.

100 = 49 + 51 (both composite and relatively prime to 100)

99 = 49 + 50 (both composite and relatively prime to 99)

98 = 33 + 65 (both composite and relatively prime to 98)

97 = 49 + 48 (both composite and relatively prime to 97)

96 cannot be written as the sum of two relatively prime composite numbers

95 = 49 + 46 (both composite and relatively prime to 95)

94 = 49 + 45 (both composite and relatively prime to 94)

93 = 49 + 44 (both composite and relatively prime to 93)

92 = 35 + 57 (both composite and relatively prime to 92)

91 = 45 + 46 (both composite and relatively prime to 91)

90 cannot be written as the sum of two relatively prime composite numbers

Hence

function(90,100) = FALSE, the highest integer in the range that failed the test is 96

At least one integer in the range failed the test and so the function returns FALSE, to begin with. Even though both 90 and 96 failed the test, only 96 is displayed in the output as it has the highest value.

function(91,95) = TRUE, all integers in the range passed the test

Since every integer passed the test in the range (91,95) the function returns TRUE, to begin with.

Extra information: I'm pretty much sure that this algorithm will involve some kind of For/While loops, potentially with some nested IF statements. But as I've stated in the comments below, I'm just not sure how to piece it all together to achieve what I aiming for.


Solution

  • Actually your sequence of numbers which can't be written as a sum of 2 composite and relatively primes is finite -> http://oeis.org/A096076 And pretty small too. Notice that if (and only if) a 2 numbers are coprime to each other <-> the sum is coprime to any of them. for proof: https://proofwiki.org/wiki/Numbers_are_Coprime_iff_Sum_is_Coprime_to_Both

    so best solution would just check if the interval contains one of them, and if it does take the smallest one and return it

    for more detail and information I've found a few stuff about it: https://mathoverflow.net/questions/354215/not-the-sum-of-two-relatively-prime-composite-numbers https://math.stackexchange.com/questions/1455165/numbers-as-sum-of-two-relatively-prime-composite-numbers