views:

266

answers:

2

hi , i need to solve this question but im stuck at getting the factors , but what i need to do is...

A positive number n is consecutive-factored if and only if it has factors, i and j where i > 1, j > 1 and j = i + 1. Write a function named isConsecutiveFactored that returns 1 if its argument is consecutive-factored, otherwise it returns 0. the function signature is int isConsectiveFactored(int n)

the function signature is int isConsectiveFactored(int n) Example

If n is 24 return 1 because 24 = 2*3*4 and 3 = 2 + 1

If n is 105 return 0 because 105 = 3*5*7 and 5 != 3+1 and 7 != 5+1

If n is 90 return 1 because factors of 90 include 2 and 3 and 3 = 2 + 1

so far ive been able to get the factor i.e if the number is 24 then ive been able to get 2 and 12 , but im stuck there and blanked....

 using System;
  using System.Collections.Generic;
  using System.Text;

 namespace isConsecutiveFactored
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(isConsecutiveFactored(24));
    }

    private static int isConsecutiveFactored(int p)
    {


        foreach (int a1 in getFactor(24))
        {
            Console.WriteLine(a1);
        }



        return 0;

    }

    private static List<int> getFactor(int p)
    {
        List<int> factor = new List<int>();
        int max = (int)Math.Sqrt(p);
        for (int i = 1; i <= max; i++)
        {
            if (i != 0)
            {
                if ((p % i) == 0)
                {

                    if (i != max)
                    {
                        if ((p / i) != 1 && (p / i) != p)
                        {
                            factor.Add(i);
                            factor.Add(p / i);
                            //Console.WriteLine((p / i) + "  " + "this is the factor");
                        }
                    }

                }
            }

            //
        }
        List<int> fac = factor.GetRange(0, 2);


        return fac;
    }
}

}

can anybody help me with this .....

A: 
    public static bool IsConsecutiveFactored(int number)
    {
        var ints = Factor(number);
        return (from i in ints join s in ints on i equals s + 1 
                where i > 1 && s > 1
                select i).Count() > 0;
    }

    public static IEnumerable<int> Factor(int number)
    {
        int max = (int)Math.Sqrt(number);  //round down
        for (int factor = 1; factor <= max; ++factor)
        { //test from 1 to the square root, or the int below it, inclusive.
            if (number % factor == 0)
            {
                yield return factor;
                if (factor != max)
                { // Don't add the square root twice!  Thanks Jon
                    yield return number / factor;
                }
            }
        }
    }

But you should really do your homework yourself, and I couldn't bring myself to return an int.

Yuriy Faktorovich
+3  A: 

Try the following

public static bool IsConsequtiveFactor(int number) {
  var factors = GetFactors(number);
  int? last = null;
  foreach ( var cur in factors ) {
    if ( last.HasValue && last.Value == cur - 1 ) {
      return true;
    }
    last = cur;
  }  
}

public static IEnumerable<int> GetFactors(int number) {
  int max = (int)Math.Sqrt(number);
  return Enumerable
    .Range(2,max-2)
    .Where(x => 0 == number % x);
}
JaredPar