Results 1 to 8 of 8

Thread:

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date: Nov:2007
    Location: Sofia
    Posts: 1,400

    Code:
    static void Main(string[] args)
    {
        Dictionary<int, double> elementValue = new Dictionary<int, double>();
        int elementNumber = 0;
    
        int value = 0;
        do
        {
            Console.WriteLine("Enter elements of the sequence:");
            if (Int32.TryParse(Console.ReadLine(), out value))
            {
                break;
            }
        } while (true);
    
        for (int i = 1; i < value; i++)
        {
            if ((i % 2) == 0)
            {
                elementNumber++;
                if (elementNumber != 1)
                {
                    //var newValue = elementValue[elementNumber -1] % 2;
                    var newValue = 3 * elementValue[elementNumber - 1] + 1;
                    if (newValue == 1)
                    {
                        Console.WriteLine("We have found that the element at which the An is 1 is " + elementNumber);
                        break;
                    }
                    elementValue.Add(elementNumber, newValue);
                }
                else
                {
                    elementValue.Add(elementNumber, elementNumber);
                }
            }
        }
    }
    2 .
    , :

    0, 1, 2 ... N -
    - N e , N+1 = aN div 2
    - N e , N+1 = 3*aN + 1
    Last edited by PhrozenOne; 27th April 2010 at 20:20.

  2. #2
    Registered User
    Join Date: Dec:2007
    Location: Sofia
    Posts: 366
    ?

  3. #3
    Banned
    Join Date: Nov:2007
    Location: Sofia
    Posts: 1,400
    , ?

  4. #4
    Registered User
    Join Date: Dec:2007
    Location: Sofia
    Posts: 366
    ,

    - Collatz, C# . .

    - , .

  5. #5
    Registered User
    Join Date: Feb:2006
    Location: Plovdiv
    Posts: 392
    Code:
    		static IEnumerable<int> Hahaha(int val)
    		{
    			while(val > 1)
    			{
    				yield return val;
    				val = (val % 2 == 0) ? val / 2 : val * 3 + 1;
    			}
    			
    			yield return 1;
    		}

  6. #6
    Banned
    Join Date: Nov:2007
    Location: Sofia
    Posts: 1,400
    , N, aN 1.

  7. #7
    Registered User
    Join Date: Feb:2006
    Location: Plovdiv
    Posts: 392
    Quote Originally Posted by PhrozenOne View Post
    , N, aN 1.
    2 , .

  8. #8
    Banned
    Join Date: Nov:2007
    Location: Sofia
    Posts: 1,400
    ... ...

    a0, a1, ... aN :
    - N e , an+1 = an div 2
    - aN e , n+1 = 3*an +1

    "n", an = 1
    . , , ... , .

    Code:
     static void Main(string[] args)
            {
               
                int value = 0;
    
                do
                {
                    Console.WriteLine("Enter starting element element of the sequence:");
                    if (Int32.TryParse(Console.ReadLine(), out value))
                    {
                        if (value > 0)
                            break;
                    }
                } while (true);
                 
                int sum = value;
                int sequence = 0;
                do
                {
                    if ((sum % 2) != 0)
                    {
                        sum = (3 * sum) + 1;
                    }
                    else
                    {
                        sum = sum / 2;
                    }
                    sequence++;
                    Console.WriteLine(sum);
                } 
                while (sum > 1);
                Console.Write("Reached 1 in n:" + sequence);
                Console.ReadLine();
            }
    , .
    Last edited by PhrozenOne; 28th April 2010 at 01:13.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |