Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5964481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:26:25+00:00 2026-05-22T19:26:25+00:00

I am new here. I am trying to solve this exercise Problem 18 just

  • 0

I am new here. I am trying to solve this exercise Problem 18 just for reinforcing my solving skills. I’ve already coded the answer. The task asks for “How many of the primes below 1,000,000 have the sum of their digits equal to the number of days in a fortnight?” (a fortnight is 14 days). My answers is 16708, but it is wrong. I hope you can help me. I don’t know what my error is. I have 2 methods, 1 for generating the primes, and another for counting the digits of each prime.

This is my code:

import java.util.ArrayList;
import java.util.List;

public class Problema18 {

    public static void main(String args[]) {
        ArrayList<Integer> num = primes();
        System.out.println(num);
        count(primes());
    }

    public static ArrayList<Integer> primes() {
        List<Integer> primes = new ArrayList<Integer>();
        primes.add(2);

        for (int i = 3; i <= 1000000; i += 2) {
            boolean isPrime = true;

            int stoppingPoint = (int) (Math.pow(i, 0.5) + 1);
            for (int p : primes) {
                if (i % p == 0) {
                    isPrime = false;
                    break;
                }
                if (p > stoppingPoint) { break; }
            }
            if (isPrime) { primes.add(i); }
        }
        // System.out.println(primes);
        return (ArrayList<Integer>) primes;
        //System.out.println(primes.size());
    }

    public static void count(ArrayList<Integer> num) {
        int count = 0;

        for (int i = 0; i <= num.size() - 1; i++) {
            int number = num.get(i);
            String num1 = String.valueOf(number);
            int sum = 0;

            for (int j = 0; j < num1.length(); j++) {
                sum = Integer.parseInt(num1.charAt(j) + "") + sum;
                if (sum == 14) { count++; }
            }
            System.out.println(sum);
        }
        System.out.println(count);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:26:26+00:00Added an answer on May 22, 2026 at 7:26 pm

    You should check whether sum == 14 outside the inner for loop. What happens now is that you also count those primes for which the sum of digits is larger than 14 but the sum of the digits in some prefix of the prime is equal to 14.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Java and trying to solve the problem below So this
I've been trying to solve this problem for quite sometime but I am having
Well, I can't seem to solve this XSLT 1.0 take-home quiz problem. Here's the
I have been trying to solve this Concurrent Programming exam exercise (in C#): Knowing
Been trying to solve this error for ages now, hoping that someone here can
I was trying to solve this problem in UVa, but I get a NullPointerException
I'm trying to troubleshoot and solve this problem: the server I'm working on (php
This is my second try to solve this problem. My first try was here
Quite new to maven here so let me explain first what I am trying
I'm trying to subclass DataList to accept a new Command from embedded LinkButtons. Here's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.