Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

I asked this question on programmers 10 days back but wasn't able to get the answer that I wanted.

Write program in java to print the squares of first 25 integers(1-25) without using any variables.

I am not looking for any theory explaining concepts about java or functional programming. I just want to know if its possible in java, the way the author has described in the original context.

--------Edit-------------------- Actually making it mutable variable makes it quite easy. The original context was

(take 25 (squares-of (integers))) Notice that it has no variables. Indeed, it has nothing more than three functions and one constant. Try writing the squares of integers in Java without using a variable. Oh, there’s probably a way to do it, but it certainly isn’t natural, and it wouldn’t read as nicely as my program above.

So try writing same program without using any variables.

share|improve this question
2  
Isn't it just a simple recursion? I don't know much Java, so I don't want to write a real solution, but something like printsq(from, to) { print(from*from); if (from < to) printsq(from+1, to); } –  ugoren Feb 25 '13 at 13:36
2  
Strongly related to Implement a sorting algorithm with no change, and I continue to think the whole business displays a lack of deep understanding about what your computer is doing. Furthermore, you got answers on programmers that are completely equivalent to what a pure functional language does under the hood. –  dmckee Feb 25 '13 at 15:39
1  
@dmckee: disagree. this is about code golf. you do it because it's fun and challenge. this is not about good design, but about creativity finding a solution to a problem that might or might not be useful to solve. –  Atmocreations May 13 '13 at 18:30
add comment

3 Answers

up vote 5 down vote accepted

Ugly hack in 216 characters, this does not even use method args:

class M{static void m(){System.out.println((Thread.currentThread().getStackTrace().length-2)*(Thread.currentThread().getStackTrace().length-2));if(Thread.currentThread().getStackTrace().length-2<25)m();}static {m();}}

Formatted, this looks like:

class M {
    static void m()
    {
        System.out.println((Thread.currentThread().getStackTrace().length-2)*(Thread.currentThread().getStackTrace().length-2));
        if (Thread.currentThread().getStackTrace().length-2 < 25) m();
    }
    public static void main(String[] a)
    {
        m();
    }
    // We can comply fully with the question (no variables),
    // but then cannot avoid the "java.lang.NoSuchMethodError: main":
    static 
    {
        m();
    }
}

For those who don't understand: It takes the stacks length squares it. This works because I'm recursively calling m(), always increasing the stack by 1. Maybe this could be written even shorter.

share|improve this answer
    
great job. finally a no variable answer !! –  minusSeven May 13 '13 at 12:43
add comment

Obligatory cheat:

public class PrintTwentyFiveSquares {

    public static void main() {
        System.out.println("1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625");
    }

}
share|improve this answer
    
Haha nice one :) –  aditsu Feb 25 '13 at 15:00
add comment

How about this?

public class Squares {
    public static void printSquares(final int n) {
        if (n > 1) {
            printSquares(n - 1);
        }
        System.out.println(n * n);
    }

    public static void main(final String... args) {
        printSquares(25);
    }
}

Now that you completely changed the question, my solution depends on whether you consider function arguments to be variables or not. I'd say it still stands.

I should add that the Closure solution depends on the pre-availability of several functions in the standard library. The same functions could be written in Java, leading to a nearly identical main program:

import java.util.AbstractList;
import java.util.List;

public class Squares2 {
    public static List<Integer> integers() {
        return new AbstractList<Integer>() {
            @Override
            public Integer get(final int index) {
                return index + 1;
            }

            @Override
            public int size() {
                return Integer.MAX_VALUE;
            }
        };
    }

    public static List<Integer> squaresOf(final List<Integer> l) {
        return new AbstractList<Integer>() {
            @Override
            public Integer get(final int index) {
                return l.get(index) * l.get(index);
            }

            @Override
            public int size() {
                return l.size();
            }
        };
    }

    public static List<Integer> take(final int n, final List<Integer> l) {
        return l.subList(0, n);
    }

    public static void main(final String... args) {
        System.out.println(take(25, squaresOf(integers())));
    }
}
share|improve this answer
2  
Besides, it looks like you already received plenty of good answers on "programmers", discussing all the aspects of the problem. Not sure what else you are looking for. –  aditsu Feb 25 '13 at 14:58
    
I am only interested in knowing if its remotely possible to do so without using variables as it is claimed by the author. Maybe be some kind of hack or cheat or maybe something else. I never thought it was possible to write a java program without using main method. Its possible actually. –  minusSeven Feb 25 '13 at 16:33
    
Well, you have 3 different solutions here (so far), all without using variables. Take your pick. –  aditsu Feb 25 '13 at 16:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.