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

  • SEARCH
  • Home
  • 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 9233449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:33:22+00:00 2026-06-18T06:33:22+00:00

import java.math.BigInteger; import java.util.ArrayList; public class Factorial { public static int[] bitVector(int n) {

  • 0
import java.math.BigInteger;
import java.util.ArrayList;

public class Factorial {

    public static int[] bitVector(int n) {
        ArrayList<Integer> bitList = new ArrayList<Integer>();
        BigInteger input = computeFactorial(n);
        System.out.println(input);
        BigInteger[] result = input.divideAndRemainder(new BigInteger(String.valueOf(2)));
        if (result[0].intValue()==0) {return new int[]{result[1].intValue()};}
        else {
            bitList.add(result[1].intValue());
        }
        while(result[0].intValue() != 0) {
            result = result[0].divideAndRemainder(new BigInteger(String.valueOf(2)));
            bitList.add(result[1].intValue());
        }
        int[] array = new int[bitList.size()];
        for (int i=0; i<array.length; i++) {
            array[i]=bitList.get(i).intValue();
        }
        return array;
    }

    public static BigInteger computeFactorial(int n) {
        if (n==0) {
            return new BigInteger(String.valueOf(1));
        } else {
            return new BigInteger(String.valueOf(n)).multiply(computeFactorial(n-1));
        }
    }

    public static void main(String[] args) {
        int[] bitVector = bitVector(35);
        for (int bit: bitVector)
        System.out.print(bit+" ");
        System.out.println();
    }
}

The code above works fine when the input to bitVector is no bigger than 35. However, when I pass 36 as a parameter to bitVector, all but one bit are gone in the output.

I have potentially ruled out the following causes:

  1. It may have nothing to do with the BigInteger type since it was designed to never overflow.

  2. It may not be related to memory usage of the program, which uses only 380M at runtime.

  3. I print out the value of computeFactorial(36), which looks good.

What on earth is going on there?

  • 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-18T06:33:24+00:00Added an answer on June 18, 2026 at 6:33 am

    So what you are trying to do is

    public static String factorialAsBinary(int n) {
        BigInteger bi = BigInteger.ONE;
        for (; n > 1; n--)
            bi = bi.multiply(BigInteger.valueOf(n));
        return bi.toString(2);
    }
    
    public static void main(String... args) {
        String fact36 = factorialAsBinary(36);
        for (char ch : fact36.toCharArray())
            System.out.print(ch + " ");
        System.out.println();
    }
    

    which prints

    1 0 0 0 1 0 0 0 1 0 1 0 0 1 1 0 0 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 0 1 1 1 0 1 0 1 0 0 0 0 0 1 1 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0 1 1 0 0 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    What on earth is going on there?

    Your code is much more complicated than it needs to be which also makes it easier to make mistakes and harder to understand.

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

Sidebar

Related Questions

import java.math.BigInteger; import java.util.concurrent.*; public class MultiThreadedFib { private ExecutorService executorService; public MultiThreadedFib(final int
here's the code: import java.lang.System; import java.lang.Math; public class ArrayFunHouseTwo { public static int[]
I have a problem import java.math.BigInteger; import java.io.*; import java.util.*; import java.lang.*; public class
import java.lang.Math; public class Homework2 { public static void main(String[] args){ int d1 =
import java.math.BigInteger; import java.util.HashMap; /** * * @author cypronmaya */ public class test {
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
import java.math.BigInteger; public class Rational extends Number implements Comparable { // Data fields for
My code sample: import java.math.*; public class x { public static void main(String[] args)

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.