Package clothcat.ssta.P041_050

Source Code of clothcat.ssta.P041_050.P50

package clothcat.ssta.P041_050;

import clothcat.ssta.lib.Primes;
import java.util.List;
import java.util.TreeSet;

/**
*
*/
public class P50 {

    public static void main(String[] args) {
        System.out.println(new P50().solve());
    }

    private long solve() {
        final int MAX = 1000000;
        List<Long> p = new Primes(MAX).getPrimeList();
        TreeSet<Long> p1 = new TreeSet<Long>();
        for(Long l : p){
            p1.add(l);
        }
        long maxsum = 0;
        long maxcount = 0;

        for (int i = 0; i < p.size(); i++) {
            // stop once we hacve no chance of improvement
            if (maxsum > MAX - p.get(i)) {
                break;
            }
            long sum = 0;
            int count = 0;
            for (int j = i; sum < MAX && j < p.size(); j++) {
                sum += p.get(j);
                count++;
                if (p1.contains(sum)) {
                    if (count > maxcount) {
                        maxcount = count;
                        maxsum = sum;
                    }
                }
            }
        }
        return maxsum;
    }
}
TOP

Related Classes of clothcat.ssta.P041_050.P50

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.