tags:

views:

115

answers:

3

Not A Homework Question, We Are Still Studying Loops At School encountered in a programming challenge ... Start The number 666 is considered to be the occult "number of the beast" and is a well used number in all major apocalypse themed blockbuster movies. However the number 666 can't always be used in the script so numbers such as 1666 are used instead. Let us call the numbers containing at least three contiguous sixes beastly numbers. The first few beastly numbers are 666, 1666, 2666, 3666, 4666, 5666...

Given a 1-based index n, my program should return the nth beastly number.

Definition

  • Class: ApocalypseSomeday
  • Method: getNth
    • Parameters: int
    • Returns: int
    • Method signature: int getNth(int n) (be sure your method is public)

Constraints

  • n will be between 1 and 10000, inclusive

Examples

  1. 2 returns: 1666
  2. 3 returns: 2666
  3. 6 returns: 5666
  4. 187 returns: 66666
  5. 500 returns: 166699

Not a problem given by a teacher. I found it in a programming challenge C++. My progress so far

public class ApocalypseSomeday
{
  public int getNth(int n)
  {
       int i = 0, j = 0,k = 0;
       int s = 1,c = 1;
       int r = 666;
       while (s < n)
       {
            k = 0;
            while ((c % 10000) == 6666 && s < n && k < 10000)
            {
                r = c * 10000 - 6000 + k;
                k++;
                s++;
            }
A: 

Since there are no performance constraints mentioned and the size of the input is quite small, the simplest option is to use brute force: count from 1 upwards and check each number to see if it contains 666. When you find n such numbers, return the last one you found.

The simplest (but slow) way to check if a number contains 666 is to convert it to a string and search for the substring '666'. Again, because of the limited size of the input and lack of performance constraints, this should be sufficient.

It is probably faster to make this check using arithmetic operations. In Python you could do it like this:

def contains666(x):
    while x >= 666:
        if x % 1000 == 666:
            return True
        x /= 10
    return False

If you need your program to be as fast as possible you could precalculate the answer for each possible value of n and hardcode the answer into your program. Then you can find the result for any n with a simple indexing operation.

Mark Byers
what's the simplest way other than string searching? int((x mod 10^n)/10^(n-3))?
sreservoir
@sreservoir: See update.
Mark Byers
Yeah ! I asked the organisers if I could use Python but C++ was the norm my friend....
wiseKID
that code is ridiculously easy to turn into C. my haskell would be significantly harder. (let me work on that python.)
sreservoir
@md.halim: I'm not going to write it in C++ for you because I think it will be a useful exercise for you to learn how how the code works and write it yourself rather than just copy+pasting it into your <s>homework</s> programming challenge.
Mark Byers
(string comparison is probably closer to what you'd actually be doing by hand.)
sreservoir
I'm done my friend. Check above.
wiseKID
A: 
public class ApocalypseSomeday {
     public int getNth(int n) {
          int i = 0, j = 0,k = 0;
          int s = 1,c = 1;
          int r = 666;
          while (s < n) {
               k = 0;
               while ((c % 10000) == 6666 && s < n && k < 10000) {
                   r = c * 10000 - 6000 + k;
                   k++;
                   s++;
               }
               if (s == n) return r;
               if (k == 10000) {
                   c++;
                   continue;
               }
               k = 0;
               while ((c % 1000) == 666 && s < n && k < 1000) {
                   r = c * 1000 + k;
                   k++;
                   s++;
               }
               if (s == n) return r;
               if (k == 1000) {
                   c++;
                   continue;
               }
               k = 0;
               while ((c % 100) == 66 && s < n && k < 100) {
                   r = c * 1000 + 600 + k;
                   k++;
                   s++;
               }
               if (s == n) return r;
               if (k == 100) {
                   c++;
                   continue;
               }
               k = 0;
               while ((c % 10) == 6 && s < n && k < 10) {
                   r = c * 1000 + 660 + k;
                   k++;
                   s++;
               }
               if (s == n) return r;
               if (k == 10) {
                   c++;
                   continue;
               }
               r = c * 1000 + 666;
               c++;
               s++;
          }
          return r;
     }
}
wiseKID
A: 

This works-

class Program
{
    static void Main()
    {
        var test = getNth(500);
    }

    static int getNth(int n)
    {
        return DevilNumbers().Take(n).LastOrDefault();
    }

    static IEnumerable<int> DevilNumbers()
    {
        for (int i = 666;; i++)
        {
            if (i.ToString().Contains("666"))
            {
                yield return i;
            }
        }
    }
}
Quinn351
Just realised this was supposed to be a C++ question not C#
Quinn351