10924 - Prime Words

All about problems in Volume 109. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

sum_sim
New poster
Posts: 4
Joined: Fri May 21, 2010 12:13 pm

Re: 10924 - Prime Words

Post by sum_sim »

Hey plz help me...um getting WA...here is my code...please check it..

Code: Select all

#include<iostream>
using namespace std;
int j;
bool prime_check(int p)
{
    if(p == 1) return false;
    else if(p == 2) return true;
    else if(p%2==0) return false;
	else if( p > 2)
		for(j=3; j*j<=p; j += 2)
			if( p%j == 0){ return false; break;}

    return true;
}

int main()
{
    char simon[21];
    int c=0,i,sum=0;
    while(cin.getline(simon,21,'\n'))
    {
    sum=0;
       for(i=0;simon[i]!='\0';i++)
        {
            if(simon[i] >='A' && simon[i] <= 'Z'){sum += (simon[i]-38);}
            if(simon[i] >='a' && simon[i] <= 'z') {sum += (simon[i]-96);}
        }

        if(prime_check(sum)) printf("It is a prime word.");
	
        else printf("It is not a prime word.");
		
    }


    return 0;
}
sum_sim
New poster
Posts: 4
Joined: Fri May 21, 2010 12:13 pm

Re: 10924 - Prime Words

Post by sum_sim »

I got AC....1 should take as a prime number here...:)
DD
Experienced poster
Posts: 145
Joined: Thu Aug 14, 2003 8:42 am
Location: Mountain View, California
Contact:

Re: 10924 - Prime Words

Post by DD »

I think the only thing we should take case for this problem is that "1 is prime".
Have you ever...
  • Wanted to work at best companies?
  • Struggled with interview problems that could be solved in 15 minutes?
  • Wished you could study real-world problems?
If so, you need to read Elements of Programming Interviews.
topboy1998
New poster
Posts: 1
Joined: Fri Oct 26, 2012 10:29 am

10924 - spelling error

Post by topboy1998 »

Hi,
There is a spelling error in the input section of the problem 10924 - "Prime Words" it says
input is terminated by enf of file
while it should be :
input is terminated by end of file
So, I request editing the problem statement to correct the spelling error
Thanks in regards.
alimbubt
New poster
Posts: 39
Joined: Tue Aug 07, 2012 10:40 pm
Location: BUBT,Dhaka, Bangladesh
Contact:

Re: 10924 - Prime Words

Post by alimbubt »

Code: Select all

The only tricky test case for this problem is 1 is considered as prime number which has been stated on the top of the problem.
" A prime number is a number that has only two divisors: itself and the number one. Examples of prime numbers are: 1, 2, 3, 5, 17, 101 and 10007."
Give me six hours to chop down a tree and I will spend the first four sharpening the axe...(BUBT ILLUSION)
http://uhunt.felix-halim.net/id/155497
http://onlyprogramming.wordpress.com/
triplemzim
New poster
Posts: 48
Joined: Sat Apr 06, 2013 6:02 pm

Re: 10924 - Prime Words

Post by triplemzim »

As the problem statement is a prime number has only two divisors (1 and itself) , 1 should not be added to the list of prime numbers....
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10924 - Prime Words

Post by brianfry713 »

1 is a prime in this problem, so a is a prime word.
Check input and AC output for thousands of problems on uDebug!
triplemzim
New poster
Posts: 48
Joined: Sat Apr 06, 2013 6:02 pm

Re: 10924 - Prime Words

Post by triplemzim »

I was trying to say that the problem statement is contradictory...
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 10924 - Prime Words

Post by uDebug »

Replying to follow the thread.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
arefinnomi
New poster
Posts: 1
Joined: Fri Mar 07, 2014 9:13 pm

10924 - Prime Words

Post by arefinnomi »

i have tested many case.
this code have passed successfully.
but got WA.

Code: Select all

#include <stdio.h>
char prime(int n)
{
    int a;
    char re;
    double remainder;
    for(a=2; a<n; a++)
    {
        remainder=n%a;
        if(remainder==0)
        {
            break;
        }
    }
    if(remainder==0)
    {
        re = 'n';
    }
    else
    {
        re='p';
    }

    return re;
}

int char_num(char a)
{
    int num;
    if(a>='a'&&a<='z')
    {
        num= a-96;
    }
    else num = a-38;
    return num;
}

int main()
{
    char a[20];
    while(gets(a))
    {
        int n, total=0;
        for(n=0; n<20; n++)
        {
            if(a[n]=='\0') break;
            else
            {
                total=total + char_num(a[n]);
            }
        }
        char ch = prime(total);
        if(ch=='p') printf("It is a prime word.");
        else printf("It is not a prime word.");
        printf("\n");
    }
    return 0;
}

what's wrong with is code.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10924 - Prime Words

Post by brianfry713 »

Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!
musfiqur.cse
New poster
Posts: 10
Joined: Tue Dec 03, 2013 8:48 pm

Re: 10924 - Prime Words

Post by musfiqur.cse »

Help Me please getting WA ! :( :'(

Code: Select all

#include<bits/stdc++.h>
using namespace std;
#define PI acos(-1.0)
#define ll long long
#define MAX 1000007
bool a[MAX];
  void isPrime()
  {
      ll i,j;
       for(i=3;i*i<=MAX;i+=2)
       {
           if(a[i]==false){
            for(j=i*i;j<=MAX;j+=i+i){
            a[j]=true;
            }
         }

       }
  }
  bool checkprime(ll n)
  {
       if(n<1)  return false;
     else if(n==1) return true;
      else if(n==2) return true;
      else if(n%2==0) return false;
     else return a[n]==false;
  }


int main()
{
ll p,q,r,s,i,j,k,sum;
isPrime();
string ss;


while(!cin.eof())

{

    cin>>ss;
    sum=0;
    for(i=0;i<ss.size();i++)
    {
     if(isupper(ss[i]))
        sum+=(ll)(ss[i]-'A')+27;
     else
        sum+=(ll)(ss[i]-'a')+1;

    }


    if(checkprime(sum))
        cout<<"It is a prime word."<<endl;
    else
        cout<<"It is not a prime word."<<endl;



}


return 0;
}



brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10924 - Prime Words

Post by brianfry713 »

Don't use cin.eof(), there will be a newline char at the end of the last line. Use something like this instead:
while(cin >> ss)
Check input and AC output for thousands of problems on uDebug!
liya
New poster
Posts: 10
Joined: Fri Jan 23, 2015 2:36 pm

Re: 10924 - Prime Words

Post by liya »

prob:10924.......it's getting WA.....

Code: Select all

#include<stdio.h>
int main()
{
    char str[20];
    int i,sum,count;
   while((scanf("%s",&str))!=EOF)
   {
      sum=0;
    for(i=0;str[i]!='\0';i++)
    {
    if(str[i]>='a'&&str[i]<='z')
        {
            sum=sum+str[i]-'a'+1;

        }
        else
        {
            sum=sum+str[i]-'A'+27;
        }
    }
    count=0;
    for(i=2;i<=sum/2;i++)
    {
        if(sum%i==0)
        {
            count++;
            break;
        }
    }
    if(count==0||sum==1)
    {
        printf("It is a prime word\n");
    }
    else if(count==1)
    {
        printf("It is not a prime word\n");
    }


}
}
Last edited by brianfry713 on Fri Jan 23, 2015 9:40 pm, edited 1 time in total.
Reason: Added code blocks
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10924 - Prime Words

Post by brianfry713 »

You are missing the period at the end of each line.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 109 (10900-10999)”