Project Euler 24 using java Logical Error

aMighty

I am trying to solve problem 24 https://projecteuler.net/problem=24 of the project euler and I have spent a lot of time on this problem but still no success.

But now it's not about the problem, I wanted to know what wrong with my method.

I am using simple permutation to get the leftmost digit and getting the remaining value from the term to be found (by subtracting i.e. using modulo operator).

Java Code:

import java.util.ArrayList;

public class LexOrder
{

public static int factorial(int num)
{
    int res=1;
    if(num <= 1)
        return 1;

    while(num > 1)  
    {
        res *= num--;   
    }
    return res;
}

public static ArrayList<Integer> addValue(int digit, ArrayList<Integer> al)
{
    int temp=0, count=0;    
    while( count <= digit )
    {
        if( al.contains(count) )
            temp++;
     count++;
    }

    int val = digit+temp;

    //checking weather the new number exists in the ArrayList or not.
    while(true)
    {
        if(! al.contains(val) )
        {
            al.add(val);
            break;
        }
        else
        {
            val++;
        }
    }
    return al;
}

public static void main(String args[])
{
    ArrayList<Integer> al = new ArrayList<Integer>();   
    int index = 999999; 
    int numOfDigit = 10;

    if( factorial( numOfDigit ) > index && index >= 0)
    {
            System.out.println("Index Validated");
    }
    else
    {
            System.out.println("Index out of bounds");
            System.exit(0);
    }

    int digit, count=1;
    while( index !=0  )
    {
        digit = ( index / factorial( numOfDigit - count ) );
        al = addValue(digit, al);

        index = ( index % factorial( numOfDigit - count ) );

        if(index == 0)
            break;
        count++;
    }

    // Adding value in ascending order.
    int temp =0;
    while( al.size() < numOfDigit )
    {

        if(!al.contains(temp))
            al.add(temp);

    temp++;
    }

    System.out.println(al);
}
}

Output: [2, 7, 8, 3, 9, 1, 4, 5, 6, 0]

Output should be: 2783915460

Linus

So without being able to walk you through the exact numbers I can tell something is off with the way you manage your digits:

int temp=0, count=0;    
while( count <= digit )
{
    if( al.contains(count) )
        temp++;
 count++;
}

int val = digit+temp;

This approach in particular fails to check whether or not some of the numbers (particularly those between digit and digit+temp) are already in your array list.

I was able to fix it by counting on the number of elements not used instead:

int val=0, count=0;
while( count < digit)
{   if( !al.contains(val++) )
        count++;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Project Euler 24 using java Logical Error

From Dev

Java: Heap space error, Euler project 14

From Dev

Java ATM project logical error(s)

From Dev

Project Euler #23 in Java

From Dev

Project Euler 2 in Java

From Dev

Project Euler #8 in java

From Dev

Project Euler 14 Java

From Dev

Project Euler #23 in Java

From Dev

Project Euler #22 Java

From Dev

Java Code for Project Euler #12

From Dev

Basics of Java : Project Euler problems

From Dev

Java project: number of students that passed or fail?? Logical error

From Dev

ActionListener Logical Error (Java)

From Dev

Logical error in Java programming

From Dev

Python - Project Euler #80, understanding the error

From Dev

Project Euler prob 10 using haskell

From Dev

project euler 14 using the state monad

From Dev

project euler 14 using the state monad

From Dev

Trying to Solve Project's Euler's First Questions Using Java, Am Having Trouble Returning the Result

From Dev

Project Euler #3 in Java; program not outputting result

From Dev

Incorrect Output for Project Euler #2 - Java

From Dev

Project Euler #5 in Java: stuck in the end of program

From Dev

Strange Logical Error in Java Code

From Dev

two logical java projects in one physical project

From Dev

Prime factorisation segmentation error, for Project Euler Prob #3

From Dev

Can't find error in code (Project Euler #11 (Haskell))

From Dev

How to download all of project euler's problems using wget

From Java

Project Euler problem 10, wrong answer but why (Java)

From Dev

Project Euler 14(Collatz Conjecture), issue implementing cache in Java