Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prime_pair_sets
{
    class Program
    {
        static void Main(string[] args)
        {
            int mc = 5;
            List<int> primes = new List<int>();
            int test = 2;
            int count = 0;
            int test2 = 0;

            while(primes.Count < mc)
            {
                test = MyMath.Here.NextPrime(test);
                int a = 0;
                foreach(int el in primes)
                {
                    if(MyMath.Here.IsPrime(test*Convert.ToInt32(Math.Pow(10,el.ToString().Length))+el)&&             MyMath.Here.IsPrime(el*Convert.ToInt32(Math.Pow(10,test.ToString().Length)) + test))
                    {
                        a++;
                    }
                    else
                    {
                        break;
                    }
                }


                if(a == primes.Count)
                {
                    test2 = test;
                    primes.Add(test);
                    count++;
                }
            }

            foreach(int a in primes)
            {
                Console.WriteLine(a);
            }
        }
    }
}


How to check all possbile values and choose the one with smallest sum?
in the code NextPrime and IsPrime is working perfectly they are not a problem

What I have tried:

I have tried to get the 4 nuymbers in example .By changing 'mc' in code to 4 you can get that numbers. when it is 5 code is not working...
Posted
Updated 6-Jul-17 21:33pm
v4
Comments
Richard MacCutchan 7-Jul-17 3:33am    
You have not explained what the code is supposed to do, and neither have you explained what is not working. Also the following statement does not make a lot of sense:

if(MyMath.Here.IsPrime(test*Convert.ToInt32(Math.Pow(10,el.ToString().Length))+el)&& MyMath.Here.IsPrime(el*Convert.ToInt32(Math.Pow(10,test.ToString().Length)) + test))

Why are you converting test to a string and then calculating something based on the string length?
Member 13294470 7-Jul-17 3:38am    
it have to be in one row, and it makes sanse
Member 13294470 7-Jul-17 3:39am    
if you have for example 3 and 109 you have to check if 1093 and 3109 are primes or not.
Richard MacCutchan 7-Jul-17 3:58am    
Well you will need to use your debugger to find out what is going on as your code is incomplete so we cannot test it.
Member 13294470 7-Jul-17 4:24am    
code is working .
problem is with the idea i think, wrong algorithm.
do you have facbook? I'll send you NextPrime and IsPrime

1 solution

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 13294470 7-Jul-17 2:51am    
i know what it is, and how to use it
Patrice T 7-Jul-17 3:04am    
But you are unable to see what is going wrong.
"why it is not working?"
is not informative.
Member 13294470 7-Jul-17 3:31am    
well, i think i know what is wrong.
my code doesn't matter what 'mc' is first four numbers are always 3,7,109,673.
and maybe there aren't any more numbers with which these numbers could be pair.
a new question...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900