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 7905805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:38:47+00:00 2026-06-03T10:38:47+00:00

I’ve been experimenting with Python as a begninner for the past few hours. I

  • 0

I’ve been experimenting with Python as a begninner for the past few hours. I wrote a recursive function, that returns recurse(x) as x! in Python and in Java, to compare the two. The two pieces of code are identical, but for some reason, the Python one works, whereas the Java one does not. In Python, I wrote:

x = int(raw_input("Enter: "))

def recurse(num):
    if num != 0:
        num = num * recurse(num-1)
    else:
        return 1

    return num 

print recurse(x)

Where variable num multiplies itself by num-1 until it reaches 0, and outputs the result. In Java, the code is very similar, only longer:

public class Default {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args){

            System.out.print("Enter: ");
            int x = input.nextInt();
            System.out.print(recurse(x));


}

    public static int recurse(int num){

    if(num != 0){
    num = num * recurse(num - 1);
    } else {
        return 1;
    }

    return num;

}

}

If I enter 25, the Python Code returns 1.5511x10E25, which is the correct answer, but the Java code returns 2,076,180,480, which is not the correct answer, and I’m not sure why.

Both codes go about the same process:

  • Check if num is zero
  • If num is not zero
    • num = num multiplied by the recursion of num – 1
  • If num is zero
    • Return 1, ending that stack of recurse calls, and causing every returned num to begin multiplying
  • return num

There are no brackets in python; I thought that somehow changed things, so I removed brackets from the Java code, but it didn’t change. Changing the boolean (num != 0) to (num > 0 ) didn’t change anything either. Adding an if statement to the else provided more context, but the value was still the same.

Printing the values of num at every point gives an idea of how the function goes wrong:

Python:

1
2
6
24
120
720
5040
40320
362880
3628800
39916800
479001600
6227020800
87178291200
1307674368000
20922789888000
355687428096000
6402373705728000
121645100408832000
2432902008176640000
51090942171709440000
1124000727777607680000
25852016738884976640000
620448401733239439360000
15511210043330985984000000
15511210043330985984000000

A steady increase. In the Java:

 1
 2 
 6  
 24
 120 
 720
 5040
 40320
 362880
 3628800
 39916800
 479001600
 1932053504
 1278945280
 2004310016
 2004189184
-288522240
-898433024
 109641728
-2102132736 
-1195114496
-522715136
 862453760
-775946240
 2076180480
 2076180480

Not a steady increase. In fact, num is returning negative numbers, as though the function is returning negative numbers, even though num shouldn’t get be getting below zero.

Both Python and Java codes are going about the same procedure, yet they are returning wildly different values. Why is this happening?

  • 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-03T10:38:49+00:00Added an answer on June 3, 2026 at 10:38 am

    Two words – integer overflow

    While not an expert in python, I assume it may expand the size of the integer type according to its needs.

    In Java, however, the size of an int type is fixed – 32bit, and since int is signed, we actually have only 31 bits to represent positive numbers. Once the number you assign is bigger than the maximum, it overflows the int (which is – there is no place to represent the whole number).

    While in the C language the behavior in such case is undefined, in Java it is well defined, and it just takes the least 4 bytes of the result.

    For example:

    System.out.println(Integer.MAX_VALUE + 1);
    // Integer.MAX_VALUE = 0x7fffffff
    

    results in:

    -2147483648
    // 0x7fffffff + 1 = 0x800000000
    

    Edit

    Just to make it clearer, here is another example. The following code:

    int a = 0x12345678;
    int b = 0x12345678;
    System.out.println("a*b as int multiplication (overflown) [DECIMAL]: " + (a*b));
    System.out.println("a*b as int multiplication (overflown) [HEX]: 0x" + Integer.toHexString(a*b));
    System.out.println("a*b as long multiplication (overflown) [DECIMAL]: " + ((long)a*b));
    System.out.println("a*b as long multiplication (overflown) [HEX]: 0x" + Long.toHexString((long)a*b));
    

    outputs:

    a*b as int multiplication (overflown) [DECIMAL]: 502585408
    a*b as int multiplication (overflown) [HEX]: 0x1df4d840
    a*b as long multiplication (overflown) [DECIMAL]: 93281312872650816
    a*b as long multiplication (overflown) [HEX]: 0x14b66dc1df4d840
    

    And you can see that the second output is the least 4 bytes of the 4 output

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I

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.