# check my code

i have problem in problem PC05.its running in my compiler but showing wrong answer in my code. please check it. my id is pmaurya_93

There are no links given in there!

1 Like

Hello @pmaurya_93,

From what I saw from your code, one thing you need to do, is to initialize all arrays before creating them, otherwise you might get Runtime errors due to that…

Other than that I think you might be complicating what is so simple…

I will try to code this problem now and tell you about it soon

A tip: Notice how the range (L,U) is so small

Best regards,

Bruno

``````1
1
10000
``````

Correct Output : 3010

Hint: Problem is much simple. It’s direct implementation. Check numbers from l to u. Sums up the digits and check its prime or not.

Hello again @pmaurya_93,

As @sobhagya said, this problem is actually pretty straightforward

The key observation here is that the range of prime numbers to check is extremely small (in fact, you only need the first eleven primes!!!), so a simple brute-force solution will do the trick!

Also, I would advise you to write your code on a more clear and possibly modular approach! This might be a matter of personal opinion, but think that other people might want to read your code and learn from it, so, write it in the most clear manner you can

Below is the Accepted code I wrote:

``````#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

static int v[11] = {2,3,5,7,11,13,17,19,23,29,31};

int sum(int N)
{
int ans = 0;
while(N>0)
{
ans += (N%10);
N/=10;
}
return ans;
}

int main()
{
int t;
cin >> t;
while(t--){
int L,U;
int ans = 0;
cin >> L >> U;
for(int i = L; i <= U; i++)
{
for(int j = 0; j <= 10; j++)
{
if(sum(i)==v[j])
ans++;
}
}
cout << ans << endl;
}
return 0;
}
``````

Actually, imho writing clear code becomes as important as coming up with the right logic, especially when more people might want to read it

Best regards,

Bruno

Sadly on real competition you don’t have time for “clear” code. But even on contest you must follow some clarity of your code, because it becomes impossible to debug.

But imho over time each coder build his own style to write code, that is readable for him.

And writing code for many people to see and learn from it, that is a new different level (and very difficult).

@tijoforyou earlier there was a link…dude

Well, if you allow me to difer from your opinion, I believe that simply dividing code in functions and if possible write a little header explaining the goal of each function, or goal of program is very important and not that time consuming All my solutions as setter here on Codechef are commented and clear

Also, on a sidenote, imho knowledge is only as good as how many people it can get to, otherwise, it’s useless

That’s why the super detailed posts on the challenge problem done by @mugurelionut are so appreciated, because he is able to write the beauty of his ideas in the most clear way I’ve ever read… And his codes can be tricky to follow, but, only by reading his texts we can learn immensely

2 Likes

In fact there were no links. There was no space between “it.” and “my”, causing it to be shown as “it.my”, which was rendered as a hyperlink (which of course, is dead)

Yes, when you’re problem setter is great, that you’re programms are easy to read and understand.

On the other hand, when you’re contestant you don’t think about such things as comments. But your code must look someway. For instance you shouldn’t use copy-paste because it’s creating duplicated bugs hard to find out. In that case you should create function for it. Otherwise function is not necessary.

But I agree, that when you use sum(n) as function is more clear to anyone. What I’m trying to say is, that readability of code is not so important and it comes with experiences naturally.

And this quote: "
Also, on a sidenote, imho knowledge is only as good as how many people it can get to, otherwise, it’s useless ;)" is really nice and I can’t agree more.

I’m trying everytime, when I explainig something to someone, express my solution as clear as possible. But I don’t know how deep should I go in explanation. Because some algorithm, proof or idea can be simple for me, when I encountered it many times, but for readers it can be difficult. And also, if I explain something very precissly, it can be too long and boring, because reader already know most of that.

Yes, I agree, but, as I’ve written editorials and lately I’ve been writing some tutorials, I guess that going long and deep on ANY explanation is also very, very good for YOU!! After all, after you try to explain something to someone, even if you don’t notice it, your knowledge on that specific topic will get better

1 Like

“knowledge is only as good as how many people it can get to, otherwise, it’s useless ;)”

How True it is!!

That’s true. Because you can’t explain something properly, if you don’t understand it completely. And even then it’s pretty hard

@kuruma I also agree 100% with the fact that explaining (or teaching or all other forms of sharing our knowledge to someone) actually helps us get better and better in the topic.

I have always taken the Floyd-Warshall algorithm for granted, and never knew how/why it worked. One day, a few of my juniors wanted me to explain it to them. I looked up some explanation on the algo, and tried to deliver it to them. But, what happened was that, in the process of explanation, I actually understood what is happening in the algo, and was able to explain it better, than I originally could have!

1 Like

Exactly!!! This is the “magic” of explaining or wanting to explain!!

Idk exactly how or why, but it seems that trying to deliver what we know in our head to others actually might train other parts of our brain and possibly form new connections such that we have a new “insight” on the topic we believed we understood

When I learn something new or find something well-written, I always think: “How would I explain this in a neat way?”

Doing this search and these questions has helped me a lot

2 Likes

@tijoforyou That happens to me too.

Funny is, when I prepering some lecture and I get stucked on some proof or idea, that I heared or use before, but suddenly I didn’t believe to it. Why it works? I asked myself. Than I spent some time with it, cracked it and then I’m able to deliver it to my audience without hesitations or any concerns.

1 Like
//