4, 8, 15, 16, 23, 42

  • 4, 8, 15, 16, 23, 42



    Write a program that outputs this sequence of numbers infinitely. However, The Numbers must not appear in your source code anywhere.



    The following is not a valid Java program to output The Numbers because The Numbers appear in its source code:



    class TheNumbers {
    public static void main(String[] args) {
    for(int n = 0;;) System.out.println(
    n == 4 ? n = 8 :
    n == 8 ? n = 15 :
    n == 15 ? n = 16 :
    n == 16 ? n = 23 :
    n == 23 ? n = 42 : (n = 4)
    );
    }
    }


    The definition of "The Numbers must not appear in your source code" is as follows:




    • You must not use the numeral 4.

    • You must not use the numeral 8.

    • You must not use the numeral 1 followed by the numeral 5.

    • You must not use the numeral 1 followed by the numeral 6.

    • You must not use the numeral 2 followed by the numeral 3.



    If your language ignores certain characters that can be placed between the numerals, it's not a valid substitution. So for example if your language interprets the literal 1_5 as 15, this would count as the numeral 1 followed by the numeral 5.



    Alternative bases are included in the restriction, so for example:




    • Binary 100 can't be used as a substitute for 4.

    • Octal 10 can't be used as a substitute for 8.

    • Hexadecimal F can't be used as a substitute for 15.



    Therefore, the following is a valid (but not very inspired) Java program to output The Numbers because The Numbers do not appear in its source code:



    class TheNumbers {
    public static void main(String[] args) {
    for(int n = '*';;) {
    System.out.println(n -= '&');
    System.out.println(n *= 2);
    System.out.println(n += 7);
    System.out.println(++n);
    System.out.println(n += 7);
    System.out.println(n += 19);
    }
    }
    }


    Note that in that program, '*' and '&' are substituted for the integers 42 and 38, because otherwise the numerals 4 and 8 would appear in its source code.



    The definition of "outputs the sequence infinitely" is open to interpretation. So, for example, a program that outputs glyphs getting smaller until they are "infinitely" small would be valid.



    Kudos if you are able to generate the sequence in some way that's not basically hard-coding each number.





    This is a popularity contest, so be creative. The answer with the most up votes on March 26th is the winner.


    I can count 6 downvotes but no comments :/

    “You may not use the numeral 1 followed by the numeral 5” – So “1_5” is valid? Because `ruby` and `perl` just interprets it as 15.

    @Vereos I was just thinking the same thing (... and 8 answers). I don't think the question is *great*, but I gave an upvote (and a comment) to even it out a bit.

    "Should I post these suggestions?" If you are changing the rules of the contest, no. If you just want to provide some ideas to get people to try approaching the challenge differently, go for it! Also, bear in mind there are no bonuses in a popularity contest...the votes reign supreme.

    This might have been funny as code-golf...

    @Vereos, "This is a stupid question" isn't very constructive, which might be why no-one posted it as a comment.

    There are 11 types of people in this world: those that watched Lost, those that didn't, and those that don't understand binary.

    @PeterTaylor For sure, but newcomers mostly will not get that and leave the site instead of trying to improve their future questions. I guess that `This isn't an interesting question, IMHO, since the solution is pretty trivial. Please post in the sandbox next time.` would be way better than `This is a stupid question.`, but that's just my personal opinion.

    @Vereos 'Pretty trivial to solve' isn't even particularly constructive considering that many popularity contests are trivial to solve and the point is not to do the bare minimum.

    @manatwork That's a good point, I forgot about underscores. Not sure if I should edit the OP. I thought about loopholes like this but ultimately figured that answers that take advantage of loopholes would be unpopular so it didn't make much of a difference.

    @Radiodef The point of my sentence was not that; I just wanted to enhance the fact that in my opinion a downvote should always be paired to a (more or less) kind comment.

    @Vereos I get what you meant, I am just saying I still don't understand where the down votes came from.

    @Radiodef aha I see what you did there... haven't seen Lost in quite some time!

    When I saw the edit that added a link to "*fix a pseudo-random generator*", I thought it would point to my answer below. Instead it links to a page on SO :(

    @Geobits I didn't see you had actually already done it until after the edit but you got an up vote from me since it was a thing I had in mind. : ) I tried to find a seed myself but wasn't able to.

    You just didn't look far enough. It's around 8.45e9 ;)

    Some formulas are available at OEIS. `a(i)=a(i-1)+a(i-3)+a(i-5)` seems like it could help.

    Thou shall not count to two, unless they proceed to three! :)

    I notice the question does not prohibit outputting *other* numbers. So at least according to infinite-monkey-theory an unadulterated pseudo-random number generator should do the trick.

    const int four = 1+1+1+1;

    @kojiro I left it open to interpretation so I suppose that would be a valid entry. "Outputs infinitely" implies you have infinite time to output them. But it would be difficult to demonstrate it works and unfiltered output wouldn't be obvious on its own.

    @squeamishossifrage Haven't watched lost, understand binary. Uh oh!

    Out of curiosity - what is the background for this particular sequence?

    @ThorbjørnRavnAndersen The numbers appear in the television show Lost. There is actually not really a whole lot more to it than that. They are a plot device and part of the lore.

    PHP (80chr) `$i=2;while($i){$i=$i%9==6?4:$i<<1;if($i%9==7)echo $i-1,$i,2+$i+=5;else echo $i;}` can't post answer???

  • Radiodef

    Radiodef Correct answer

    7 years ago

    Java



    I decided to add another entry since this is completely different from my first one (which was more like an example).



    This program calculates the average of an array entered by the user...



    import java.util.Scanner;

    public class Numbers {
    public static double getSum(int[] nums) {
    double sum = 0;
    if(nums.length > 0) {
    for(int i = 0; i <= nums.length; i++) {
    sum += nums[i];
    }
    }

    return sum;
    }

    public static double getAverage(int[] nums) { return getSum(nums) / nums.length; }
    public static long roundAverage(int[] nums) { return Math.round(getAverage(nums)); }

    private static void beginLoop(int[] nums) {
    if(nums == null) {
    return;
    }

    long avg = roundAverage(nums);
    System.out.println("enter nums for average");
    System.out.println("example:");
    System.out.print("array is " + nums[0]);
    for(int i = 1; i <= nums.length; i++) {
    System.out.print(", " + nums[i]);
    }

    System.out.println();
    System.out.println("avg is " + avg);
    }

    private static int[] example = { 1, 2, 7, 9, };

    public static void main(String[] args) {
    boolean done = false;
    while(!done) {
    try {
    int[] nums = example;
    beginLoop(nums);

    nums = getInput();
    if(nums == null) {
    done = true;
    } else {
    System.out.println("avg is " + getAverage(nums));
    }
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    }

    static int[] getInput() {
    Scanner in = new Scanner(System.in);
    System.out.print("enter length of array to average or 0 to exit: ");
    int length = in.nextInt();
    if(length == 0) {
    return null;

    } else {
    int[] nums = new int[length];
    for(int i = 0; i <= nums.length; i++) {
    System.out.print("enter number for index " + i + ": ");
    nums[i] = in.nextInt();
    }
    return nums;
    }
    }
    }


    ...or does it?



    java.lang.ArrayIndexOutOfBoundsException: 4
    at Numbers.getSum(Numbers.java:8)
    at Numbers.getAverage(Numbers.java:15)
    at Numbers.roundAverage(Numbers.java:16)
    at Numbers.beginLoop(Numbers.java:23)
    at Numbers.main(Numbers.java:42)
    java.lang.ArrayIndexOutOfBoundsException: 4
    at Numbers.getSum(Numbers.java:8)
    at Numbers.getAverage(Numbers.java:15)
    at Numbers.roundAverage(Numbers.java:16)
    at Numbers.beginLoop(Numbers.java:23)
    at Numbers.main(Numbers.java:42)
    java.lang.ArrayIndexOutOfBoundsException: 4
    at Numbers.getSum(Numbers.java:8)
    ...

    This is great! I would not have thought of something like that.

    Wow, beautiful ! Great idea ;)

    Genius! Though the output is a bit verbose, but I guess that has to do with the language you chose here. ;)

    Brilliantly done!

    Just when I thought the Python "lizt=Lost plot" one couldn't be topped...

    this really deserves to be the top answer

    I retracted my vote as I noticed the answer came from OP. What a way to brag. (This happens many times in PCG, though, indeed this site is a very suitable place to brag, haha)

    @justhalf Actually it bugs me this was the top answer for a little while there. It's no fun to win my own question.

    I helped you to be the second! I believe you got the code first before constructing this challenge based on the unexpected output of your code?

    @justhalf It seems it was for naught. The votes just keep coming! Also, if you are asking if I came up with this code by accident: no. I had the idea for this after I posted the challenge. I think my head would explode if I saw this in a legit stack trace. : )

    Loved this one! :)

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM