Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Prime Number program not working
New Posts  All Forums:Forum Nav:

Prime Number program not working

post #1 of 4
Thread Starter 
Well I am using nested loops in ADA to find the 10,001 prime number. and here is my current code.
Code:
with Ada.Integer_Text_IO;

procedure test is

   P : Integer;
   C : Integer;
   S : Integer;

begin

   P := 1; -- Prime Number
   C := 0; -- Counter
   S := 1; -- What I am dividing by

   O_Loop :
   loop
      if (P rem S) = 0 then
         P := P + 1;
         S := 2;
      else
         I_Loop :
   loop
      if (P rem S) /= 0 then
         S := S + 1;
         C := C + 1;
            exit I_Loop when (P rem S) = 0;
            exit O_Loop when C = 10_001;
      end if;
   end loop I_Loop;
      end if;
   end loop O_Loop;
   Ada.Integer_Text_IO.Put (Item => P);
end test;

And while it should take 10,001 counter add ups to reach the prime number 104,743 it is taking 497,000,000 counter add ups.

I need to find a better way for finding the prime numbers than my current setup. I know the counter most likely isn't in the right place either thus taking so many counter add ups to reach the prime number.

Any help would be greatly appricated.

Thanks,
Bob
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
post #2 of 4
Well, let me try to understand and feel to correct me, your code is set up like this:
Code:
P,C,S variable declaration
While loop ( c < 10001 )
     if ( P rem S = 0 )
     else
          While loop ( P rem S = 0 )
               if ( P rem S /= 0 )
               end if
          end loop
     end if
end loop

Now, one small thing:
"1" isn't considered a primer number, so you should just ignore it and begin P at "2" instead of "1"

Second, your counter is adding up every time ( P rem S ) is different from "0", from my understanding, it only should add up every time it finds a prime numbers. IE: Since 3%2 is /=0 then it adds up a counter, that's wrong since that doesn't mean it has found a prime number.

I have a code in mind, but it's totally different from yours, and pretty basic I think, if you want I can post the idea here.
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
post #3 of 4
Thread Starter 
Please any help would be greatly appricated.

Also should I be using square roots to find it?
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
post #4 of 4
Well, square root helps alot, my code without square roots takes around 17 secs while using square root takes 0 secs according to netbeans.

This is how I pretty much did it, will write the idea since I have no idea about ADA:

<- will be my assignation
= will be equal to
sqrt(x) will refer to the square root of x
Code:
Integer Num <- 2 // This will be the number that I will be increasing
Integer Counter <- 0 // Primer counter
Integer Divisors // The ammount of divisors a number has
Integer I // The numbers I'll be dividing by
While Loop ( Counter < 10001 ) // This will be the loop until it finds the 10,001 prime number
  Divisors <- 2 // If it's a prime number then it it divisible by 2 numbers
  I <- 2; // It's always divisible by 1 so there is no need to check 1
  While Loop ( I <= sqrt(Num) && Divisors = 2) // If it finds already another number below or equal to its square root, then it isn't a prime. Also, if it finds another divisor, there is no need to keep looking
    If ( Num rem I = 0 ) // Checks if I find a divisor
      Divisors <- Divisors + 1 // If it does, add it
    End If
    I <- I + 1 // Adds to I to to check the next number
  End While Loop
  If ( Divisors = 2 ) // If the divisors stayed at "2" then it's a prime number
    Counter <- Counter + 1 // Therefore I add "1" to my counters
  End If
  Num <- Num + 1 // Adds to number to check the next one
End While Loop
Write Num - 1 // Write Num-1 since I add +1 at the end of my loop

That's how I did it, if you have any questions feel free to ask
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Prime Number program not working