Friedman Numbers

 I got the idea from this post from a Fermat's Library tweet:


You can find more about Friedman numbers here: https://en.wikipedia.org/wiki/Friedman_number. The "joke" in the tweet was that I had a proof about the fact that there are infinite such numbers - and the reality is that there are! But I don't really have the proof :) The code though to generate some Friedman numbers is below. I say some because I'm not taking into account parenthesis, so the set below is a subset of the Friedman numbers. It is computational intense since I need to create all the permutations of the number and then check all the possible variations with all the operators. but it finds nice ones, like this:

8^5+7*2+3 = 32785

Code is down below, cheers, ACC.

*Notice that I'm using info.lundin.math library, you'll need to Nuget it first


public bool IsFriedmanNumber(int number)
{
    return IsFriedmanNumber(number.ToString(), "", new Hashtable());
}

private bool IsFriedmanNumber(string number,
                                string candidate,
                                Hashtable indexUsed)
{
    if (String.IsNullOrEmpty(number)) return false;

    if (candidate.Length == number.Length)
    {
        if (IsNiceFriedmanNumber(candidate, "", Int32.Parse(number)))
        {
            return true;
        }
        return false;
    }

    for (int i = 0; i < number.Length; i++)
    {
        if (!indexUsed.ContainsKey(i))
        {
            indexUsed.Add(i, true);
            if (IsFriedmanNumber(number, number[i].ToString() + candidate, indexUsed)) return true;
            indexUsed.Remove(i);
        }
    }

    return false;
}

public bool IsNiceFriedmanNumber(int number)
{
    return IsNiceFriedmanNumber(number.ToString(), "", number);
}

private bool IsNiceFriedmanNumber(string number,
                                    string expression,
                                    int target)
{
    if (String.IsNullOrEmpty(number))
    {
        expression = expression.Substring(1);
        if (!target.ToString().Equals(expression))
        {
            ExpressionParser eparser = new ExpressionParser();
            string[] exps = { expression, "-" + expression };
            double eval = 0.0;
            foreach (string exp in exps)
            {
                eval = eparser.Parse(exp);
                if (eval == target)
                {
                    Console.WriteLine("FOUND");
                    Console.WriteLine("{0} = {1}", exp, eval);
                    return true;
                }
            }
        }
        return false;
    }

    string[] ops = { "+", "-", "*", "/", "^" };
    for (int i = 0; i < number.Length; i++)
    {
        string partial = number.Substring(0, i + 1);
        foreach (string op in ops)
            if (IsNiceFriedmanNumber(number.Substring(i + 1), expression + op + partial, target)) return true;
    }

    return false;
}

Comments

Popular posts from this blog

Advent of Code - Day 6, 2024: BFS and FSM

Golang vs. C#: performance characteristics (simple case study)

Advent of Code - Day 7, 2024: Backtracking and Eval