-3
\$\begingroup\$

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.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ 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); } \$\endgroup\$
    – ugoren
    Feb 25, 2013 at 13:36
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Feb 25, 2013 at 15:39
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ May 13, 2013 at 18:30

3 Answers 3

10
\$\begingroup\$

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");
    }

}
\$\endgroup\$
1
5
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ great job. finally a no variable answer !! \$\endgroup\$
    – minusSeven
    May 13, 2013 at 12:43
1
\$\begingroup\$

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())));
    }
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Feb 25, 2013 at 14:58
  • \$\begingroup\$ 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. \$\endgroup\$
    – minusSeven
    Feb 25, 2013 at 16:33
  • \$\begingroup\$ Well, you have 3 different solutions here (so far), all without using variables. Take your pick. \$\endgroup\$ Feb 25, 2013 at 16:38

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