(Given a choice, I wouldn't read a single shortish input using file (or stream or channel) IO - just use an argument to main.)
static void func(long num)
is neither documented nor named suggestively. It doesn't even live up to it's name, not returning a value, but using a class data member for result communication. This prevents multiple&concurrent use - instead, return a value.
RE60K (and jschnei) showed "analysis from the sum formula".
Alternatively, start from the sum target: to be a sum of two consecutive natural numbers, it needs to be odd. Tabulating this for several sum lengths:
# condition min
2 1 % 2 1
3 0 % 3 3
4 2 % 4 6
5 0 % 5 10
6 3 % 6 15 3, too
7 0 % 7 21
8 4 % 8 28
9 0 % 9 36 3, too
10 5 % 10 45 5, too
11 0 % 11 55
12 6 % 12 66 3, too
15 0 % 15 130 3, 5, too
using that, I get 1, 0, 2, 0, 1, 2, 1, 0, 2, 2, 1, 1, 1, 1, 4, 0, 1, 2, 1, 1, 4… ways to "consecutively sum to" n=1..21 (throwing this at OEIS yields - nothing. Going from 2 or more consecutive positive integers
to 1: oeis.org/search?q=2,1,3,1,2,3,2,1,3,3,2,2,2,2,5,1,2,3,2,2,5 - in the FORMULA
paragraph, you may find the generating function - and a description:)
number of odd divisors (including n), plus one if n is a triangular number (sum from 1..k for some k).
/** For a given <em>n</em>, compute the number of ways
* to sum more than one consecutive natural number yielding <em>n</em>. */
// owes to an answer by RE60K@CR
class ConsSum {
public static void main(String[] args) throws NumberFormatException {
final long
from = args.length < 1 ? 42 : Long.parseLong(args[0]),
num = args.length < 2 ? from : Long.parseLong(args[1]);
System.out.println("There are");
for (long sum = from ; sum <= num ; sum++) {
final int ways = getConsSumWays(sum);
System.out.println(ways
+ Ending.forSingular(" way", ways) + " to get " + sum);
}
}
/** @return the number of ways to sum more than one consecutive
* natural number yielding <code>n</code>. */
private static int getConsSumWays(long n) {
int count = 0;
final long triangularCandidate = (long) (Math.sqrt(8*n + 1)/2);
final boolean triangular
= n == (triangularCandidate*(triangularCandidate+1))/2;
for (long divisor = 3 ; divisor <= n/2 ; divisor += 2)
if (0 == n % divisor)
count++;
return count + ((int)n & 1) + (triangular ? 1 : 0);
}
}
// for laughs:
/** stemming helpers etc */
class Ending {
static final java.util.regex.Pattern PLURALISER
= java.util.regex.Pattern.compile(".*(?:"
+ "(?<ies>[^aeiou]y)"
+ "|(?<es>ch|s|sh|x|z)"
+ "|[aeiou][aeiou]fe?|(?<ves>..fe?)"
+ ")");
static final String[] Plural = {
"ies",
};
/** Doesn't (yet) incorporate exceptions e.g. from
* https://en.oxforddictionaries.com/spelling/plurals-of-nouns.
* @return given (a <code>String</code> ending in)
* an English noun in singular, return the plural form. */
static String forSingular(final String singular, final int n) {
if (n == 1)
return singular;
final java.util.regex.Matcher pluraliser
= PLURALISER.matcher(singular);
if (pluraliser.matches()) {
int start;
if (0 <= (start = pluraliser.start("ies")))
return singular.substring(0, start+1) + "ies";
if (0 <= (start = pluraliser.start("es")))
return singular + "es";
if (0 <= (start = pluraliser.start("ves")))
return singular.substring(0, start+2) + "ves";
}
// regular
return singular + 's';
}
public static void main(String[] args) {
for (String s: new String[] {
"man", "child", "try", "way", "knife", "chief", "bus" })
System.out.println(s + " -> " + forSingular(s, 2));
}
}