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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:30:40+00:00 2026-06-13T22:30:40+00:00

tl;dr : How should I be dealing with numbers like 20! * 20! in

  • 0

tl;dr: How should I be dealing with numbers like 20! * 20! in Objective-C?


I’m learning Objective-C by working through Project Euler. It’s been quite fun, but one problem I’ve been running in to is working with arbitrarily large numbers. I’m still pretty green on these things, so I don’t know why something like, say, Python, handles large numbers with ease compared to Obj-C.

Take for example Problem 15:

Starting in the top left corner of a 2 x 2 grid, there are 6 routes (without backtracking) to the bottom right corner.

How many routes are there through a 20 x 20 grid?

That’s easy. Using combinatorics:

(20+20)! / 20!(20!)
-> 815915283247897734345611269596115894272000000000 / 5919012181389927685417441689600000000
-> 137846528820

In Python:

import math  
print math.factorial(40) / (math.factorial(20) * math.factorial(20))

In Objective-C, though? I have’t yet found a way to force such a large numbers through. Using the 2 x 2 example works fine. I can get 9C4 = 126, as it should be. But how should I be dealing with numbers like 20!?

I’ve dallied with trying to use NSDecimalNumber, which appears to support more numerals per number, assuming you can convert it to Mantissa x Exponent and don’t mind loss of precision, but that didn’t prove to be too useful, as I couldn’t figure out how to have Obj-C create a Mantissa from a %llu and I do mind loss of precision.

The code I have so far properly generates the factorials, as it appears unsigned long long handles values so large, but chokes up on x * y, and thus getCombinatoricOf:20 and:20 returns 1.

#import "Problem15.h"

@implementation Problem15

- (unsigned long long)factorial:(NSNumber *)number {
    unsigned long long temp = 1;
    for (int i = [number intValue]; i > 0; i--) {
        temp *= i;
    }
    return temp;
}

- (unsigned long long)getCombinatorcOf:(NSNumber *)x and:(NSNumber *)y {
    NSNumber *n = @([x intValue] + [y intValue]);
    NSNumber *n_factorial = @([self factorial:n]);
    NSNumber *x_factorial = @([self factorial:x]);
    NSNumber *y_factorial = @([self factorial:y]);
    return ([n_factorial unsignedLongLongValue] / ([x_factorial unsignedLongLongValue] * [y_factorial unsignedLongLongValue]));
}

- (NSString *)answer {
    NSNumber *x = @5;
    NSNumber *y = @4;
    unsigned long long answer = [self getCombinatoricOf:x and:y];
    return [NSString stringWithFormat:@"\n\nProblem 15: \nHow many routes are there through a 20 x 20 grid? \n%llu", answer];
}

@end
  • 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-13T22:30:41+00:00Added an answer on June 13, 2026 at 10:30 pm

    It’s not Objective-C, but you could just use GMP just as an ordinary C library.

    There are also Objective-C wrappers fo GMP, like GMPInt.

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

Sidebar

Related Questions

Other questions dealing with use of match_parent pre 2.2 have answers stating one should
I'm dealing with an annoying database where one field contains what really should be
What's the best practice for dealing with objects in for or foreach loops? Should
I'm writing an Rcpp module an would like to return as one element of
I am working on a program that stores numbers as floats which I eventually
Should numbers from user input be quoted in MySQL queries to help avoid SQL
I've seen a few other questions dealing with localization but that doesn't seem to
I would like to build a web interface to present logdata. This should be
So far I've seen many posts dealing with equality of floating point numbers. The
I currently have a project where we are dealing with 30million+ keywords for PPC

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.