← Back to Kurakani

Programming Challenge 1

Original Post

You are given a function ‘secret()’ that accepts a single integer parameter and returns an integer. In your favorite programming language, write a program that takes one argument (a number) and determines if the secret() function is additive [secret(x+y) = secret(x) + secret(y)], for all values x and y, where x and y are prime numbers less than the 100.

helpjava11
Jun 25, 2014 1:08 PM · 10,977 views
4 Replies
batman Jun 25, 2014 1:34 PM
public static bool isPrime(int number) { for (int i = 2; i < number; i++) { if (number % i == 0) { return false; } } return true; } public int secret(int x) { if ((x < 100) && isPrime(x)) return (x); else return (0); } private void button1_Click(object sender, EventArgs e) { int val1=secret(Convert.ToInt32(textBox1.Text)); int val2 = secret(Convert.ToInt32(textBox2.Text)); if (val1 == 0 || val2 == 0) { label1.Text = "Prime number and between 0 to 100 please"; return; } int final = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text); int val3 = secret(final); if (val3 == val1 + val2) { label1.Text = "Perfect!!!"; } }
Kiddo Jun 25, 2014 2:18 PM
Am I missing something here? Is this sort-of puzzle question or just simple programming one? If the latter then batman's response is on the money. It first adds two numbers entered and it then checks whether the sum is the sum of the two numbers; ofcourse the answer will be always "Perffect!!!", unless some one doesn't enter the number correctly. My guess is that there is something more to the question. Definition of additive is too simple, did you mean something else?
helpjava11 Jun 25, 2014 3:54 PM
Kiddo bro, this is not puzzle. Conpletely coding . This is copy paste from one of the interview i took some time back.
fuckeetow Jun 25, 2014 9:33 PM
Is there some relation between "one argument (a number)" and (x, y) (or 100)? Last edited: 25-Jun-14 09:34 PM
Login to join the conversation.

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all
Ask Sajha AI Assistant