Click here to Skip to main content
15,896,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to find out the factors for 2^(n)+1 and am adding the factors into a Listbox so for Smaller numbers it is showing the O/p for Larger Numbers like if we take "n" as a three digit number it's not showing the O/P
Here is my codeL:
Button Click()
{
a = Convert.ToUInt32(textBox1.Text);
            for (i = 0; i < a; i++)
            {
                c = c * 2;
            }
            c = c + 1;
            getfactors(c);
}
public List<double> getfactors(double number)
       {
           List<double> fs = new List<double>();
          for (double i = 2; i < number / 2; i++)
           {
               if (number % i == 0)
               {
                   fs.Add(i);
                    listBox1.Items.Add(i);
               }

           }
            return fs;
       }
Posted
Updated 11-Aug-10 21:22pm
v3

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double n,final;


        private void button1_Click(object sender, EventArgs e)
        {
            n = Convert.ToDouble(textBox1.Text);
            final = Math.Pow(2, (n + 1));
            textBox2.Text = final.ToString();
            for (int i = 1; i <= final; i++)
            {
                if (final % i == 0)
                {
                    listBox1.Items.Add(i.ToString());
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            listBox1.Items.Clear();
        }
    }
}
 
Share this answer
 
Comments
Peter_in_2780 12-Aug-10 3:50am    
That might almost work, but x % 1 == 0 for any integer x. Also, checking every number from 1 to final could take a looooong time. Checking every odd number from 3 to sqrt(final) is smarter, and there are even better ways.
Vigneshb6 12-Aug-10 5:27am    
Actually it's not working
The real problem is that well before you get to 2^100, you run out of numbers that can be accurately represented in an integer or double. If you want to go much beyond about 2^50, you'll need to use a BigInteger. Read up on range and precision as they apply to computer arithmetic.
 
Share this answer
 
Comments
Vigneshb6 12-Aug-10 8:14am    
Reason for my vote of 4
Yes ur Right

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