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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:56:47+00:00 2026-06-06T18:56:47+00:00

I am trying to implement this bit of code: func factorial(x int) (result int)

  • 0

I am trying to implement this bit of code:

func factorial(x int) (result int) {
  if x == 0 {
    result = 1;
  } else {
    result = x * factorial(x - 1);
  }
  return;
}

as a big.Int so as to make it effective for larger values of x.

The following is returning a value of 0 for fmt.Println(factorial(r))

The factorial of 7 should be 5040?

Any ideas on what I am doing wrong?

package main

import "fmt"
import "math/big"

func main() {
        fmt.Println("Hello, playground")

    //n := big.NewInt(40)
    r := big.NewInt(7)

    fmt.Println(factorial(r))

}

func factorial(n *big.Int) (result *big.Int) {
    //fmt.Println("n = ", n)
    b := big.NewInt(0)
    c := big.NewInt(1)

    if n.Cmp(b) == -1 {
        result = big.NewInt(1)
    }
    if n.Cmp(b) == 0 {
        result = big.NewInt(1)
    } else {
        // return n * factorial(n - 1);
        fmt.Println("n = ", n)
        result = n.Mul(n, factorial(n.Sub(n, c)))
    }
    return result
}

This code on go playground: http://play.golang.org/p/yNlioSdxi4

  • 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-06T18:56:48+00:00Added an answer on June 6, 2026 at 6:56 pm

    In your int version, every int is distinct. But in your big.Int version, you’re actually sharing big.Int values. So when you say

    result = n.Mul(n, factorial(n.Sub(n, c)))
    

    The expression n.Sub(n, c) actually stores 0 back into n, so when n.Mul(n, ...) is evaluated, you’re basically doing 0 * 1 and you get back 0 as a result.

    Remember, the results of big.Int operations don’t just return their value, they also store them into the receiver. This is why you see repetition in expressions like n.Mul(n, c), e.g. why it takes n again as the first parameter. Because you could also sayresult.Mul(n, c) and you’d get the same value back, but it would be stored in result instead of n.

    Here is your code rewritten to avoid this problem:

    func factorial(n *big.Int) (result *big.Int) {
        //fmt.Println("n = ", n)
        b := big.NewInt(0)
        c := big.NewInt(1)
    
        if n.Cmp(b) == -1 {
            result = big.NewInt(1)
        }
        if n.Cmp(b) == 0 {
            result = big.NewInt(1)
        } else {
            // return n * factorial(n - 1);
            fmt.Println("n = ", n)
            result = new(big.Int)
            result.Set(n)
            result.Mul(result, factorial(n.Sub(n, c)))
        }
        return
    }
    

    And here is a slightly more cleaned-up/optimized version (I tried to remove extraneous allocations of big.Ints): http://play.golang.org/p/feacvk4P4O

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

Sidebar

Related Questions

I am trying to implement this demo on my local machine. http://bit.ly/4g4o1r I have
I'm trying to implement this code to have different files to load for german,
I'm trying to implement this pattern in my WinForms application (I don't like it,
I'm trying to implement this scenario using Unity and i can't figure out how
I'm trying to implement this accepted solution for displaying a custom error message: https://stackoverflow.com/a/5229581/141172
Right, I'm trying to implement this game, http://www.helicoptergame.net/ , but I'm struggling with the
I'm trying to implement the shake tutorial on this page, but I think I'm
I'm trying to implement the answer to this SO question . The problem is:
This is a problem I hit when trying to implement a game using the
I am trying to implement -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath So far this is what

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.