CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2013

    Goldbach conjecture program

    Hi, I'm still in the process of learning C-Sharp (from free internet tutorials) and I'd like to practice my skills by creating a program which recreates Goldbach's conjecture.
    I know the direction I am going to take but I am stuck due to my lack of knowledge and experience.

    So what I would like to know is: 'How can I make C-sharp list all the numbers which when added return a value equal to the given number. For example, if I give the program, the number 8, I want it to list out '1+7, 2+6, 3+5, 4+4' and so on. Is there a property or method which can do this?

    Secondly, again, is there any property or method which will check whether a number is prime or not.

    I'm not asking for a complete solution, just some advice or a nudge in the right direction would be great.
    Any help would be appreciated.

  2. #2
    Join Date
    Feb 2011
    United States

    Re: Goldbach conjecture program

    Hrm, it doesn't really sound like you are doing homework, so the best way to teach you is just to show you, I think:

    static List<Tuple<int,int>> sumPairs(int target)
        List<Tuple<int,int>> result = new List<Tuple<int, int>>();
        int bottom = 1;
        int top = target - 1;
        while( bottom <= top )
            result.Add(new Tuple<int, int>(bottom, top));
        return result;
    public static void Main(string[] args)
        List<Tuple<int, int>> pairs = sumPairs(8);
        foreach(Tuple<int, int> pair in pairs)
            Console.Write("{0}+{1}, ", pair.Item1, pair.Item2);
    (Should be substantially correct, didn't try to compile it though; might have minor syntax errors)

    As for primality testing, I think you might be able to use this numerical library to do that: http://numerics.mathdotnet.com/ Not sure though. Alternatively, it's not too hard to write a subroutine to do it yourself. If you're just testing small numbers, it's enough to just check every (odd) number less than or equal to the square root of the target number and check whether the modulus (% operator) is zero.

    If you want some more problems to test your mettle on, try Project Euler: https://projecteuler.net/problems. They are designed to be solved by computer programming usually. They range from extremely simple (earlier problems to extremely difficult (later problems).
    Best Regards,

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jan 2013

    Re: Goldbach conjecture program

    Thank you for the help. I was expecting it to be a bit less complex but I am able to make sense of it. Some minor adjustments and it will be fine. And the link to Project Euler is great, an interesting way to practice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.