Source/NFX/IntMath.cs
/// <summary>
/// Gets adjacent prime number which is less than or equal to the specified number. Arguments less than 3 yield 2;
/// </summary>
Gets adjacent prime number which is less than or equal to the specified number. Arguments less than 3 yield 2.
Gets adjacent prime number which is less than or equal to the specified number . Arguments less than 3 yield 2 .
public static int GetAdjacentPrimeNumberLessThanOrEqualTo(int number)
        {
          if (number<3) return 2;
          if (IsPrime(number)) return number;
          var odd = (number & 1) != 0;
          for(var i = odd ? number-2 : number-1; i>=3; i-=2)
          {
            var iprime = true;
            for(var j=3; (j*j) <= i; j+=2)
             if ((i % j) == 0)
             {
              iprime=false;
              break;
             }
            if (iprime) return i;
          }
          return 2;
        }
public static int GetAdjacentPrimeNumberLessThanOrEqualTo ( int number ) { if ( number < 3 ) return 2 ; if ( IsPrime ( number ) ) return number ; var odd = ( number & 1 ) != 0 ; for ( var i = odd ? number - 2 : number - 1 ; i >= 3 ; i -= 2 ) { var iprime = true ; for ( var j = 3 ; ( j * j ) <= i ; j += 2 ) if ( ( i % j ) == 0 ) { iprime = false ; break ; } if ( iprime ) return i ; } return 2 ; }
Gets adjacent prime number which is less than or equal to the specified number.
Gets adjacent prime number which is less than or equal to the specified number .
//check odd numbers starting from 3 (+2 to check only odd numbers, as even are already covered)
number
int