# Write a program to print squares of integers up to 25 in java without using variables? [closed]

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.

• 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); } Feb 25 '13 at 13:36
• 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. Feb 25 '13 at 15:39
• @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. May 13 '13 at 18:30

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

}

• Haha nice one :) Feb 25 '13 at 15:00

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()
{
}
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.

• great job. finally a no variable answer !! May 13 '13 at 12:43

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

• 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. 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. Feb 25 '13 at 16:33
• Well, you have 3 different solutions here (so far), all without using variables. Take your pick. Feb 25 '13 at 16:38