GOC105 "Birthday Party Passcode" - Editorial

PROBLEM LINK:

Contest Page | CodeChef

Contest Page | CodeChef

Author: pprasadj | CodeChef User Profile for Prasad Padalkar | CodeChef

Tester: pprasadj | CodeChef User Profile for Prasad Padalkar | CodeChef

Editorialist: Competitive Programming | Participate & Learn | CodeChef

DIFFICULTY:

EASY-MEDIUM

PROBLEM:

Madhu has arranged for a birthday party in restaurant and has invited her friends. She is interested
that only her friends are allowed in the restaurant for dinner. She decides to generate a passcode
from random number N, such that 1 <= N <= 2147483646. She uses following logic for generation
of the sequence of character for passcode.

  1. If the last number in the sequence is a prime, it terminates the sequence.
  2. Otherwise, computes the next element of the sequence as the sum of squares of digits of the last
    element.
  3. Print the sequence with all numbers together as passcode on one line.

QUICK EXPLANATION:

For example:
Input: 57
Program will write down 57, 74 (= 5^2 + 7^2), 65 (= 7^2 + 4^2), and 61 (= 6^2 + 5^2).
Number 61 is a prime.
Output: 57746561.

AUTHOR’S AND TESTER’S SOLUTIONS:


   #include<stdio.h>
#include<math.h>  
long check_prime(long);
long sqrsumdig(long); 
main()
{
   long n, primenum;
   long sqrsum=0;
     scanf("%ld",&n);
   
   while(primenum!=1)
   {
   primenum = check_prime(n); 
   if ( primenum != 1 )
   {    
         primenum = check_prime(n);
        printf("%ld", n);
        sqrsum = sqrsumdig(n); 
        n = sqrsum;
       //printf("\n\n%ld", sqrsum);
   }
   else
        printf("%ld", n);
   }
   return 0;
}
 
long check_prime(long a)
{
   long c;
 
   //for ( c = 2 ; c <= a - 1 ; c++ )
   //{ 
    //  if ( a%c == 0 )
     //return 0;
  // }
   //if ( c == a )
     // return 1;
     
     int i;
    if(a == 2) return 1;
    if(a%2 == 0) return 0;
    for(i = 3; i*i<=a; i+=2) {
        if(a%i == 0) return 0;
    }
    return 1;

}

long sqrsumdig(long num)
{
long d,sum=0;
while(num>0)
{
d=num%10;
num=num/10;
sum = sum + pow(d,2);
}
return(sum);
}