Visual c# ile çarpanlara ayırma(yardım)

Başlatan Gökhan BEKEN, 16 Haziran 2010, 18:54:31

Gökhan BEKEN

S.a
x=4000 olsun
x=a*b şeklinde ifade etmek istiyorum
karakökünü almayı denedim ama her sayının karakökü tam sayı olmuyor.
4000=100*40
gibi bir şekilde ifade etmek için hazır fonksiyon var mıdır?

Programlama.com da
şu örneği gördüm ama ben daha kısa yöntem varsa onu kullanmak istiyorum

#include <stdio.h>
int main ()
{
int i,x,m;
int a[20];
printf("bir sayi giriniz.(cikis icin 00)");
scanf("%d",&x);
while (x!=00)
{
m=0;
while(x>1)
{
for(i=2;i<=x;i++)
{
if(x%i==0)
{
a[m]=i;
m=m++;
x=x/i;
break;
}
}
}
printf("asal carpanlar:\n");
for(i=0;i<m;i++)
{
printf("\t%d.carpan=%d ",i+1,a[i]);
}
printf("\n\nbir sayi giriniz.(cikis icin 00)");
scanf("%d",&x);
}
return 0;
}


Özel mesaj okumuyorum, lütfen göndermeyin.

muhittin_kaplan

hazır bir fonksiyon olacağını düşünmüyorum.


orhanc

~~~ Program that tests IsPrime (Program.cs - C#) ~~~

using System;

class Program
{
    static void Main()
    {
        //
        // Write prime numbers between 0 and 100.
        //
        Console.WriteLine("--- Primes between 0 and 100 ---");
        for (int i = 0; i < 100; i++)
        {
            bool prime = PrimeTool.IsPrime(i);
            if (prime)
            {
                Console.Write("Prime: ");
                Console.WriteLine(i);
            }
        }
        //
        // Write prime numbers between 10000 and 10100
        //
        Console.WriteLine("--- Primes between 10000 and 10100 ---");
        for (int i = 10000; i < 10100; i++)
        {
            if (PrimeTool.IsPrime(i))
            {
                Console.Write("Prime: ");
                Console.WriteLine(i);
            }
        }
    }
}

~~~ Class that contains IsPrime (PrimeTool.cs - C#) ~~~

using System;

public static class PrimeTool
{
    public static bool IsPrime(int candidate)
    {
        // Test whether the parameter is a prime number.
        if ((candidate & 1) == 0)
        {
            if (candidate == 2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        // Note:
        // ... This version was changed to test the square.
        // ... Original version tested against the square root.
        // ... Also we exclude 1 at the very end.
        for (int i = 3; (i * i) <= candidate; i += 2)
        {
            if ((candidate % i) == 0)
            {
                return false;
            }
        }
        return candidate != 1;
    }
}

~~~ Output of the program ~~~

--- Primes between 0 and 100 ---
Prime: 2
Prime: 3
Prime: 5
Prime: 7
Prime: 11
Prime: 13
Prime: 17
Prime: 19
Prime: 23
Prime: 29
Prime: 31
Prime: 37
Prime: 41
Prime: 43
Prime: 47
Prime: 53
Prime: 59
Prime: 61
Prime: 67
Prime: 71
Prime: 73
Prime: 79
Prime: 83
Prime: 89
Prime: 97
--- Primes between 10000 and 10100 ---
Prime: 10007
Prime: 10009
Prime: 10037
Prime: 10039
Prime: 10061
Prime: 10067
Prime: 10069
Prime: 10079
Prime: 10091
Prime: 10093
Prime: 10099
i'm doing nothing... Giddy Up  http://www.drorhan.com

orhanc

Başka bir örnek

using System.Collections;
...
const int LAST_CANDIDATE = 1000;
int primes = 0;
BitArray candidates = new BitArray (LAST_CANDIDATE, true);
 
for (int i = 2; i < LAST_CANDIDATE; i++)
{
   if (candidates[i])
   {
      for (int j = i * 2; j < LAST_CANDIDATE; j += i)
         { candidates[j] = false; }
   }
}
 
for (int i = 1; i < LAST_CANDIDATE; i++)
{
   if (candidates[i])
   {
      primes++;
      Console.Out.WriteLine (i);
   }
}
 
Console.Out.WriteLine
   ("\n" + primes + " primes found in the range 2-" + LAST_CANDIDATE);
i'm doing nothing... Giddy Up  http://www.drorhan.com