Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8583033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:26:23+00:00 2026-06-11T21:26:23+00:00

I want to write a function in Java that takes as input an integer

  • 0

I want to write a function in Java that takes as input an integer and outputs every possible permutation of numbers up to that integer. For example:

f(1)

0

f(2) should output:

00
01
10
11

f(3) should output:

000
001
002
010
011
012
020
021
022
100
….
220
221
222

That is it should output all 27 permutations of the digits of the numbers 0, 1, 2.

f(4) should output
0000 0001 0002 0003 0010 … 3330 3331 3332 3333

f(5) should output
00000 00001 … 44443 44444

I have been trying to solve this problem but cannot seem to work out how to do it and keep getting confused by how many loops I need. Does anyone know how to solve this problem? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:26:24+00:00Added an answer on June 11, 2026 at 9:26 pm

    Just count and convert. I wrote something should help in an earlier answer here.

    This should be a relatively easy problem to solve.

    Essentially, you’re merely computing the set of strings Σ^5 where Σ = { 0, 1, 2 }.

    static Iterable<String> strings(final int radix, final int digits) {
      return new Iterable<String>() {
    
        public Iterator<String> iterator() {
          return new Iterator<String>() {
    
            private final String pad;
            {
              final StringBuilder buf = new StringBuilder(digits);
              for (int n = digits; n >= 0; --n) {
                buf.append('0');
              }
              pad = buf.toString();
            }
    
            private final int hi = (int) Math.pow(radix, digits);
            private int cursor;
    
            public boolean hasNext() {
              return cursor < hi;
            }
    
            public String next() {
              final String rsl = Integer.toString(cursor++, radix);
              return pad.substring(0, digits - rsl.length()) + rsl;
            }
    
            public void remove() {
              throw new UnsupportedOperationException();
            }
          };
        }
      };
    }
    

    … which can be used as follows:

    for (final String val : strings(3, 5)) {
      System.out.println(val);
    }
    

    Basically, we generate the numbers in the interval [0, 3^5), where 3 is our radix and 5 our desired string length, and then convert the numbers into ternary form. 0 becomes 00000, 3^5 becomes 100000. You must also take care to not use too large a radix, otherwise the resulting String will contain bad characters.


    The solution here is to merely call strings(n, n). Note that, depending on how large your radix or desired digit lengths are, you may wish to instead use long or even BigInteger.

    Also, since it relies on Integer.toString, make sure you remember the following caveat…

    If the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the radix 10 is used instead.

    You can see the value for Character.MIN_RADIX is 2 and MAX_RADIX is 36. If you use a radix outside of this range, it will default to 10… you will need to write your own conversion with a custom extended character set for digits. The general form of such an itoa function is as follows:

        private static final char[] ALPHABET = { '0', '1', '2', '3', ... };
    
        public static String itoa(int value, final int radix, int width) {
          final char[] buf = new char[width];
          while (width > 0) {
            buf[--width] = ALPHABET[value % radix];
            value /= radix;
          }
          return new String(buf);
        }
    

    Below is a working example for your usage (see result on ideone).

    static Iterable<String> f(final int n) {
      return strings(n, n);
    }
    
    public static void main(final String[] argv) {
      for (int n = 1; n <= 5; ++n) {
        for (final String string : f(n)) {
          System.out.printf("%s ", string);
        }
        System.out.println();
      }
    }
    

    … which produces:

    0

    00 01 10 11

    000 001 002 010 011 012 020 021 022 100 101 102 110 111 …

    0000 0001 0002 0003 0010 0011 0012 0013 0020 0021 0022 0023 0030 …

    00000 00001 00002 00003 00004 00010 00011 00012 00013 00014 00020 00021 00022 …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that takes a list of numbers and returns
I want to write a Java function that will get as input either int[]
I want to write 'twice' function that takes a function and an argument and
I want to write a function that could check every item in a list
I need to write a simple java function that takes a URL and processes
I want to write a function that identifies all the links on a particular
i want to write a function that prints multi-dimensional objects which are text (or
I want to write a function that read line by line from a socket
I am a newbie to Android and Java and want to write a function
I want to do something with ehcache in Java that I think should be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.