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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:20:08+00:00 2026-05-11T00:20:08+00:00

I’m trying to do project Euler number 219 but am failing to get a

  • 0

I’m trying to do project Euler number 219 but am failing to get a grasp of it. I’m trying to use Python which according to project Euler should be able to do it within a minute! This leads me to think that they can’t possibly want me to compute each individual bitstring since that would be too slow in Python – there has to be a sub O(n) algorithm.

I have looked at a recursive solution which stores the bitstrings possible prefixes so that it can quickly pick a new bitstring and it even considers them in groups. This only works in brute-forcing values up to a bit over 10:

cost(1) = 1 cost(2) = 5 cost(3) = 11 cost(4) = 18 cost(5) = 26 cost(6) = 35 cost(7) = 44 cost(8) = 54 cost(9) = 64 cost(10)= 74 cost(11)= 85 cost(12)= 96 

Past this, I am struggling to comprehend how reduce the problem. It is always possible to make a pattern that goes like:

1 01 001 0001 00001 00000 

But it isn’t optimal for more than 7 bitstrings. Can anyone guide me in what I should be considering?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T00:20:08+00:00Added an answer on May 11, 2026 at 12:20 am

    Brute-force is not the right way to do it. This is one of those problems where, if you know a certain thing, it’s not that hard, but if you’ve never heard of that thing, it’s practically impossible. That thing is Huffman trees.

    [Edit] Upon further review, it seems that you can’t quite build a Huffman tree on N nodes with certain frequencies, because the cost function for a string is 4*(# of 1’s) + (# of 0’s). If the cost function were the length of the string (or a multiple thereof), then you could create a Huffman tree.

    Any prefix-free code set can be represented as a Huffman-like binary tree, where each node has either 0 or 2 children, and the leaf nodes represent the codes. Given a tree with N nodes, we can construct a tree with N+1 nodes as follows:

    1. Choose the leaf node with the smallest cost, where the cost of a leaf node is 4*(# of 1’s on path from root to leaf) + (# of 0’s on path)
      • Add 2 children to that node

    Thus, if the code for the node was previously xxxx, then we remove that code from our code set (since it’s no longer a leaf), and add the two codes xxxx0 and xxxx1. The total cost of the code set has now increased by

    `cost(xxxx0) + cost(xxxx1) – cost(xxxx) = cost(0) + cost(1) + cost(xxxx) = 5 + cost(xxxx)

    Hence, mincost(N+1) <= mincost(N) + 5 + cost(cheapest code in best solution for N). My theory is that that inequality should be an equality, but I haven’t been able to prove it yet. For all of the values you’ve listed which you brute-forced, this statement is in fact an equality.

    If it is an equality, then to solve the problem what you would do is this:

    1. Start with an empty code set at a total cost of zero
      • Iterating from 1 to 109, do:
        1. Find the cheapest code in your code set
      • Split that code into two by appending 0 and 1
      • Add the cost of that code + 5 to the total cost

    If you use a priority queue, you should be able to do this in O(N log N) time. This may or may not be feasable, given the upper limit of 109.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.