Java program to find 'Lucky' numbers from 0 to n using Lists
<p>I have to write a program that finds all the lucky numbers from 0 to any number n.<br>
Here's what a lucky number is: </p>

<p>Consider the sequence of natural numbers.<br>
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 ………………………………….<br>
Removing every second number produces the sequence<br>
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 ………………………….<br>
Removing every third number produces the sequence<br>
1, 3, 7, 9, 13, 15, 19, 21, 25 ………………………….</p>

<p>This process continues indefinitely by removing the fourth, fifth…and so on, till after a fixed number of steps, certain natural numbers remain indefinitely. These are known as Lucky Numbers.</p>

<p>I decided to try using ArrayList for this. But I can't seem to figure this small bit out. I've been trying for days now.<br>
Here's the code: </p>

<pre><code>import java.util.*;
class luckyy
{
public static void main(String args[])
{

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter n: ");

    int i, n, index;
    n = scan.nextInt();     
    ArrayList &lt;Integer&gt; ele = new ArrayList &lt;Integer&gt; (n);
    //storing in a list
    for(i = 1;i&lt;=n;i++)
    {
        ele.add(i);
    }
    System.out.println(ele);
    int count = 2;
    index = 1;
    System.out.println("Size: "+ele.size());
    while(count&lt;ele.size())
    {
        for(i = 0;i&lt;ele.size();i++)
        {
            int chk = ele.get(i);
            if(chk%count == 0)
            {
                ele.remove(i);
            }                
        }
        count++;           
    }
    System.out.print(ele);        
}
}
</code></pre>

<p>This gives the output:<br>
<code>[1, 5, 7]</code><br>
When the desired output is:<br>
<code>[1, 3, 7]</code></p>

<p>So, sorry if this code is so bad that it's offensive haha...
But I would really appreciate any help. I am just starting out as a programmer, and have a lot to learn, and would love any advice. Thanks to anyone who tries to help me!</p>