Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

GOC105 "Birthday Party Passcode" - Editorial

$
0
0

PROBLEM LINK:

https://www.codechef.com/problems/GOC105
https://www.codechef.com/GMCD2016/problems/GOC105

Author:https://www.codechef.com/users/pprasadj
Tester:https://www.codechef.com/users/pprasadj
Editorialist:https://www.codechef.com/users/nileshdbit

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); }


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>