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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:32:15+00:00 2026-05-19T22:32:15+00:00

I’m looking for an algorithm (or better yet, code!) for a the generation of

  • 0

I’m looking for an algorithm (or better yet, code!) for a the generation of powers, specifically numbers with an odd exponent greater than 1: third powers, fifth powers, seventh powers, and so forth. My desired output is then

8, 27, 32, 125, 128, 216, 243, 343, 512, 1000

and so forth up to a specified limit.

I don’t want to store the powers in a list and sort them, because I’m making too many to fit in memory — hopefully the limit be 1030 or so, corresponding to a memory requirement of ≈ 1 TB.

My basic idea is to have an array holding the current number (starting at 2) for each exponent, starting with 3 and going up to the binary log of the limit. At each step I loop through the exponent array, finding the one which yields the smallest power (finding either pow(base, exponent) or more likely exponent * log(base), probably memoizing these values). At that point call the ‘output’ function, which will actually do calculations with the number but of course you don’t need to worry about that.

Of course because of the range of the numbers involved, bignums must be used — built into the language, in a library, or self-rolled. Relevant code or code snippets would be appreciated: I feel that this task is similar to some classic problems (e.g., Hamming’s problem of generating numbers that are of the form 2x3y5z) and can be solved efficiently. I’m fairly language-agnostic here: all I’ll need for my ‘output’ function are arrays, subtraction, bignum-word comparison, and a bignum integer square root function.

  • 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-05-19T22:32:15+00:00Added an answer on May 19, 2026 at 10:32 pm

    Your example is missing 64=4^3, and 729=9^3.

    You want the set of all { n^m } traversed in numerical order, m odd, n integral and n > 1. We know that (for n > 1) that increasing either n or m will increase this value, but short of calculation we can’t compare much else.

    There are two obvious “dual” ways to do this: keep track of the highest base n you consider, and for all bases less than that, the next exponent m to consider. Then pick the smallest one, and compare it to n^3. Or, the other way around — keep track of the highest exponent m, and for each exponent smaller than that, keep track of the highest base used, and find the smallest one, and compare it to adding 2^m.

    To make keeping track of these numbers efficiently, you’ll want to keep them in a priority queue. Now, you still want to minimize the number of entries in the priority queue at a time, so we’ll want to figure out which of these two methods does better job of this. It turns out that much higher n values are required to make it to a given point. At number k, the largest value of m seen will be log_2 of k, whereas the largest value of n seen will be k^(1/3).

    So, we have a priority queue with elements (v, n, m), where the value v=n^m.

    add_priority_queue(2^3, 2, 3)
    for m in 5, 7, ....
        v = 2^m
        while value(peek(queue)) <= v:
            (v1, n1, m1) = pop(queue)
            if v1 != v print v1
            add_priority_queue((n1+1)^m1, n1+1, m1)
        add_priority_queue(2^m, 2, m)
    

    Note that we need to check for v1 = v: we can have 2^9 = 512 = 8^3, and only one should be printed out, right?

    A Haskell implementation, with a random priority queue grabbed off of hackage.

    import Data.MeldableHeap
    
    dropMin q = maybe empty snd (extractMin q)
    
    numbers = generate_values (insert (2^3, 2, 3) empty) 5
    
    generate_values q m = case findMin q of
        Nothing -> []
        Just (v1, n1, m1) -> case compare v1 (2^m) of
            EQ -> generate_values (insert ((n1+1)^m1, n1+1, m1) (dropMin q)) m
            LT -> v1 : generate_values (insert ((n1+1)^m1, n1+1, m1) (dropMin q)) m
            GT -> 2^m : generate_values (insert (3^m, 3, m) q) (m + 2)
    
    main = sequence_ (map print numbers)
    

    I have a run currently at 177403008736354688547625 (that’s 23 digits) and 1.3 GB plaintext output, after 8 minutes

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

Sidebar

Related Questions

I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.