Prime Words

Assume the value of a letter is its position in the alphabet: A = 1, B = 2, ... Z = 26.

Let S be the sum of the letters in word W . If S is a prime number, then W is a prime word .

For example: "PRIME" is a prime word since it has a value of 61.

How many words in this text file are prime words ? Click here to open the file.


The answer is 157.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

The answer is 157 .

Iterate through the words in the file. For each word, iterate through the characters, convert them to integer values, and determine if their sum is prime. Use a variable v to keep track of how many prime words you find. Return the value of v once you reach the end of the file.

# Python solution

def prime_words():
    import math

    # This is one of many ways to determine if a n is prime
    def is_prime(n):
        prime = True

        # loop through numbers from 2 to the square root of n
        # if n is divisible by any of these, then it is not prime
        for divisor in range(2, math.floor(math.sqrt(n) + 1):
            if (n % divisor == 0):
                prime = False
                break
        return prime

    # Determine the value of a word based on its characters
    # This method assumes all the characters are upper-case
    def value(word):
        a_index = string.lowercase.find('a')
        sum = 0
        for ch in word:
            if (ch != '\n'):
                # determine the value of a letter by its relative position to 'a' in the alphabet
                sum += string.lowercase.find(c.lower()) - a_index + 1
        return sum

    import string
    # Loop through the words in the file, and count the number of prime words
    with open('cs_primewords.txt', 'r') as f:
        primeword_count = 0
        for word in f:
            if (is_prime(value(word))):
                primeword_count += 1
    print primeword_count
David Holcer
Mar 28, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def is_prime(a):
    return a > 1 and all(a % i for i in xrange(2, a))
filee=open('primewords.txt',"r")
times=0
for i in range(901):
    summ=0
    line=filee.readline()
    line=line.strip('\n')
    for i in line:
        summ+=(ord(i)-64)
    if is_prime(summ):
        times+=1
print times

Alex Li
Feb 16, 2015

Java code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Scanner;
import java.io.File;
public class x
{
    public static boolean sumChars(String x)
    {
        int sum = 0;
        for(int i = 0; i<x.length(); i++)
        {
            sum+=(x.charAt(i)-64);
        }
        for(int j = 2; j<=Math.sqrt(sum); j++)
        {
            if(sum%j == 0)
            {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) throws IOException
    {
        String z;
        int count = 0;
        Scanner file = new Scanner(new File("x.txt"));
        while(file.hasNext())
        {
            z = file.nextLine();
            if(sumChars(z))
            {
                count++;
            }
        }
        System.out.println(count);
    }
}

Drop TheProblem
Dec 7, 2014
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;

int sol;

void nprimo(int a)
{int b=0; int r=sqrt(a);
for(int i=2; i<=r; i++)
if(a%i == 0)
b++;
if(b==0) sol++;
}

int main(){
FILE *pf; char p[100]; int l;
    pf=fopen("input.txt","r");
    for(int i=1;i<=901;i++){
       fscanf(pf,"%s",p);
       l=strlen(p);
    int somma=0;
        for(int j=0;j<l;j++) somma+=(p[j]-64);
    nprimo(somma);
    }
  fclose(pf);
  pf=fopen("output.txt","w"); fprintf(pf,"%d\n",sol); 
  fclose(pf);
    return 0;
}

sol=157

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...