Commit 1c6ed36a authored by Verena Strobel's avatar Verena Strobel
Browse files

Erste Version des HappyNumber Algorithmus

parent f359a6a6
package happyNumber;
import java.util.ArrayList;
import java.util.List;
public class HappyNumberCalculator {
public HappyNumberCalculator()
{
// erstellt einen neuen Rechner
}
public boolean isHappyNumber(int x)
{
List<Integer> ziffern = new ArrayList<Integer>();
if(x < 10)
{
if(x == 1)
{
return true;
}
return false;
}
while (x >= 10)
{
int letzteZiffer = x%10;
x = x/10;
ziffern.add(0, letzteZiffer);
}
ziffern.add(0, x);
int ziffernQuadratSummiert = 0;
for(int i = 0; i < ziffern.size(); i++)
{
ziffernQuadratSummiert += ziffern.get(i) * ziffern.get(i);
}
if(ziffernQuadratSummiert == 1)
{
System.out.print("ziffernQuadratSummiert ist endlich 1, es handelt sich um eine HappyNumber!");
return true;
}
return isHappyNumber(ziffernQuadratSummiert);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment