Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Yet another C++ problem by yours truly....
New Posts  All Forums:Forum Nav:

# Yet another C++ problem by yours truly....

Quote:
 It has long been known that there are infinitely many prime numbers (Euclid, 300 B.C.), but another interesting question is how close together are the prime numbers, one from the next. Or, in other words, are there, for example, more or less primes between 1 and 100 than there are between 1001 and 1100? What about 1000001 and 1000100? Does the Ã¢â‚¬Å“densityÃ¢â‚¬ of prime numbers increase or decrease as the numbers themselves get larger? At first sight the primes seem to be distributed among the integers in rather a haphazard way. For example in the 100 numbers immediately before 10,000,000 there are 9 primes, while in the 100 numbers after there are only 2 primes. However, on a large scale, the way in which the primes are distributed is very regular. Legendre and Gauss both did extensive calculations of the density of primes. Gauss (who was a prodigious calculator) told a friend that whenever he had a spare 15 minutes he would spend it in counting the primes in a 'chiliad' (a range of 1000 numbers). By the end of his life it is estimated that he had counted all the primes up to about 3 million. Both Legendre and Gauss came to the conclusion that for large n the density of primes near n is about 1/log(n).
i need to write a C++ program to calculate and display the number of primes in the first 50 Ã¢â‚¬Å“chiliadsÃ¢â‚¬. The results should be as presented below...

Code:
``````Start   End     Number of Primes
1       1000    168
1001    2000    135
2001    3000    127
3001    4000    120
4001    5000    119
5001    6000    114
6001    7000    117
7001    8000    107
8001    9000    110
9001    10000   112
Ã¢â‚¬Â¦
Ã¢â‚¬Â¦
Ã¢â‚¬Â¦
49001   50000   98```
```
I don't even know where to start.... suggestions???
 good enough (13 items)
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback
 good enough (13 items)
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback
Code:
``````# include <iostream.h>
# include <conio.h>

void main()
{
clrscr();
long i,k,start=1,lim=1000,count=0,flag=1,z;
cout<<"Start\End\Number of Primes\
";

for(z=1;z<=50;z++)
{

for(i=start;i<=lim;i++)
{

for(k=2;k<=i/2;k++)
{
if((i%k)==0)
flag=0;
}//end of k loop

if(flag==1)
count++;
flag=1;

}// end of i

cout<<start<<'\'<<lim<<'\'<<count<<endl;
start+=1000;
lim+=1000;
count=0;

}// end of z loop
getch();
}end of main()```
```
There...
 Current Rig (13 items)
CPUMotherboardGraphicsRAM
Intel i7 920 Asus P6T Gigabyte 4870 1 GB Kingston 3 x 2 GB
Hard DriveOptical DriveOSPower
WD 750GB LG DVD Drive Windows 7 x64 Corsair HX 520W
 Current Rig (13 items)
CPUMotherboardGraphicsRAM
Intel i7 920 Asus P6T Gigabyte 4870 1 GB Kingston 3 x 2 GB
Hard DriveOptical DriveOSPower
WD 750GB LG DVD Drive Windows 7 x64 Corsair HX 520W
To follow up on walk_this_way did...

There is no known equation or pattern to find prime numbers. If you ever did discover one, you would be considered one of the greatest mathematicians of all time. Also, you would virtually knock out most of computer encryption.

The only reliable way of discovering prime numbers is to test the number for all possible dividable numbers. Basically, take a value and use modulus to see if there are any remainers using all previous numbers except 1. If none, then it is a prime number. If modulus is ever 0, then it is not a prime number. Move to the next number.
 Once again... (13 items)
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz]
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro
 Once again... (13 items)
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz]
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro
thanks to the head start!!! looks like its done!

i knew the thing about prime numbers... the algorithm is supposed to just go through x amount of numbers and determine if it is prime or not by finding all the factors of that number. and display the results to the user...

i'll tweak what you have up there and post back with changes... if i need any...
 good enough (13 items)
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback
 good enough (13 items)
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback
i cant get it to compile in bloodshed... Should i try visual studio?
 good enough (13 items)
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback
 good enough (13 items)
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback
before the main function call add: using namespace std;

and the last line, change from:
}end of main()

to

}//end of main()

Those were things I noticed just by glancing it over fast.
 BlackMesa (14 items)
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27"
KeyboardMouse
Filco w/ blue cherries Who needs a mouse?
 BlackMesa (14 items)
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27"
KeyboardMouse
Filco w/ blue cherries Who needs a mouse?
Yeah, that should work.

There may be logic problems though, who knows.
 The Three Budgeteers (13 items)
CPUMotherboardGraphicsRAM
Athlon 64 3700+ Sandy Biostar MATX 6100 GeForce 7600 GT 1024 MB G.SKILL PC 3200
Hard DriveOptical DriveOSMonitor
Western Digital 160 GB ASUS DVD-RW Windows XP MCE '05, Fedora 6 17'' CRT
PowerCase
FSP 450 Watt Black w/ Window
 The Three Budgeteers (13 items)
CPUMotherboardGraphicsRAM
Athlon 64 3700+ Sandy Biostar MATX 6100 GeForce 7600 GT 1024 MB G.SKILL PC 3200
Hard DriveOptical DriveOSMonitor
Western Digital 160 GB ASUS DVD-RW Windows XP MCE '05, Fedora 6 17'' CRT
PowerCase
FSP 450 Watt Black w/ Window
In the k loop you want to change k<=i/2 to k<=sqrt(i)
I don't know the C++ call for sqrt - but you get the idea
 It goes to eleven (13 items)
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7
 It goes to eleven (13 items)
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7
I think for the square root function you need to add the math lib. So I its: #include<cmath>;
Not 100% sure though, not that big of a c++ coder.
 BlackMesa (14 items)
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27"
KeyboardMouse
Filco w/ blue cherries Who needs a mouse?
 BlackMesa (14 items)
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27"
KeyboardMouse
Filco w/ blue cherries Who needs a mouse?
Heres an example.
sqrt function

double sqrt ( double x );
float sqrt ( float x );
long double sqrt ( long double x );

<cmath>

Compute square root

Returns the square root of x.

Parameters

x
Floating point value.
If the argument is negative, a domain error occurs, setting the global variable errno to the value EDOM.

Return Value
Square root of x.

Portability
In C, only the double version of this function exists with this name.

Example
Code:
``````
/* sqrt example */
#include <stdio.h>
#include <math.h>

int main ()
{
double param, result;
param = 1024.0;
result = sqrt (param);
printf ("sqrt(%lf) = %lf\
", param, result );
return 0;
}

Output:

sqrt(1024.000000) = 32.000000```
```
 The Three Budgeteers (13 items)
CPUMotherboardGraphicsRAM
Athlon 64 3700+ Sandy Biostar MATX 6100 GeForce 7600 GT 1024 MB G.SKILL PC 3200
Hard DriveOptical DriveOSMonitor
Western Digital 160 GB ASUS DVD-RW Windows XP MCE '05, Fedora 6 17'' CRT
PowerCase
FSP 450 Watt Black w/ Window
 The Three Budgeteers (13 items)
CPUMotherboardGraphicsRAM
Athlon 64 3700+ Sandy Biostar MATX 6100 GeForce 7600 GT 1024 MB G.SKILL PC 3200
Hard DriveOptical DriveOSMonitor
Western Digital 160 GB ASUS DVD-RW Windows XP MCE '05, Fedora 6 17'' CRT
PowerCase
FSP 450 Watt Black w/ Window
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• Yet another C++ problem by yours truly....
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Yet another C++ problem by yours truly....