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

-2

1

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.

minusSeven

Posted 2013-02-25T13:24:36.803

Reputation: 123

Question was closed 2016-08-15T05:14:03.377

2Isn'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 – 2013-02-25T13:36:38.230

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 --- ex-moderator kitten – 2013-02-25T15:39:58.077

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 – 2013-05-13T18:30:23.067

Answers

5

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.

Atmocreations

Posted 2013-02-25T13:24:36.803

Reputation: 166

great job. finally a no variable answer !! – minusSeven – 2013-05-13T12:43:47.783

10

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

}

histocrat

Posted 2013-02-25T13:24:36.803

Reputation: 20 600

Haha nice one :) – aditsu quit because SE is EVIL – 2013-02-25T15:00:15.763

1

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

aditsu quit because SE is EVIL

Posted 2013-02-25T13:24:36.803

Reputation: 22 326

2Besides, 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 quit because SE is EVIL – 2013-02-25T14:58:25.810

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 – 2013-02-25T16:33:26.810

Well, you have 3 different solutions here (so far), all without using variables. Take your pick. – aditsu quit because SE is EVIL – 2013-02-25T16:38:37.760