0

the goal is to find the least number that is greater than n and the digit sum of that number canbe fully divided by 11. and after I run it, it return nothing.

public class M1
{
public static int nextCRC(int n)
{
    int crc;
    for (crc=n+1;crc!=0;crc++) 
    {
        int sum=0;
        for (crc =n+1 ; crc!=0; crc = crc / 10)
        {
            int digit = crc % 10;
            sum += digit;
            if ( sum %11 == 0)
            {
                break;
            }  
        }
    }       
    return crc;             
}
public static void main(String[] args)
{
    M1 Model=new M1();
    Model.nextCRC(20);
    System.out.println(Model.nextCRC(20));
}

}
Xiaoyu Yin
  • 11
  • 1
  • Well, it's returning *something*. What's it returning vs what you expect it to return? – Carcigenicate Mar 28 '17 at 18:20
  • 4
    Well, you're running `nextCRC(20)` twice and throwing away the output the first time. You could cut your runtime in half if you don't do that. – azurefrog Mar 28 '17 at 18:20
  • 3
    @azurefrog Haven't you heard of "calculate one CRC to throw away"? – Kayaman Mar 28 '17 at 18:21
  • I expect to see a number that is greater than the n(input value) and the digit sum of it can be fully divided by 11, what I get is a time limit exceeding @Carcigenicate – Xiaoyu Yin Mar 28 '17 at 18:22
  • 3
    Possible duplicate of [Breaking out of nested loops in Java](http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java) – Max B Mar 28 '17 at 18:25
  • From what I see you are always checking if n+1 has sum divisible by 11, so you are looping indefinitely if it's not true – MWaw Mar 28 '17 at 18:25
  • @XiaoyuYin: What "time limit"? What is limiting the time that this takes to execute? How long do you expect this to take? When you debug, is something happening differently from what you expect? – David Mar 28 '17 at 18:25

2 Answers2

-1

Here you go:

import java.math.*;

public class M1
{
public static int nextVal(int n)
{   
    int sum = 0;
    int temp = n;
    while(n>9){
        sum = sum + n%10;
        n = n/10;
    }
    sum = sum+n;
    if((sum%11) == 0){
        return temp;
    }
    return nextCRC(temp+1);
}

public static int nextCRC(int n) {
    return nextVal(n+1);
}

public static void main(String[] args)
{
    M1 Model=new M1();
    Model.nextCRC(20);
    System.out.println(Model.nextCRC(40));
}

}
  • Simply writing the questioner's code for them with no explanation is a bad answer. – user2357112 Mar 28 '17 at 19:04
  • Actually I'm not looking for the answer, just wanna know what is wrong with my code. your code work, but my work has to be done in one int class – Xiaoyu Yin Mar 29 '17 at 01:53
-1

Here is my solution:

import java.util.stream.LongStream;

/*
the goal is to find the least number that is greater than n and the digit sum of that number can be fully divided by 11.
 */
public class M1 {
  private static long nextCRC(int n) {
    return LongStream.iterate(n + 1, l -> ++l)
      .filter(l -> sumOfBase10Digits(l) % 11 == 0)
      .findFirst().getAsLong();
  }

  private static long sumOfBase10Digits(final long num) {
    if (num == 0) {
      return 0;
    } else {
      return num % 10 + sumOfBase10Digits(num / 10);
    }
  }

  public static void main(String... args) {
    System.out.println(nextCRC(20));
    System.out.println(nextCRC(200));
    System.out.println(nextCRC(98765));
    System.out.println(nextCRC(92));
  }
}
Andreas
  • 4,937
  • 2
  • 25
  • 35