cdinesh1 Posted February 15, 2006 Posted February 15, 2006 hi, hey can anyone code me a program to generate prime numbers.....and also a program to check whether a number is a palindrome or not?
Klaynos Posted February 15, 2006 Posted February 15, 2006 This sounds suspiciousely like an exercise set for some programming course....
taylrl Posted February 15, 2006 Posted February 15, 2006 isnt the search for a formula for calculating prime numbers one of the great mysterys of mathematics, no wait i might be getting confused. I think i the mystery is the way of telling if a number is prime. Dont worry i will hopefully regain my sanity soon.
timo Posted February 15, 2006 Posted February 15, 2006 Telling if a number is prime and obtaining the prime factors of a number is pretty trivial (I´ve posted some metacode for that some time ago, I think). The problem is doing it quickly.
insane_alien Posted February 15, 2006 Posted February 15, 2006 I think i the mystery is the way of telling if a number is prime. Heres a clue. its prime if you can only divide it by itself and 1.
taylrl Posted February 15, 2006 Posted February 15, 2006 oh really insane alien, that helps with my understanding somewhat, but what i they number is 30 orders of magnitude or even larger, that is a lot of numbers to look at and see that they dont work.
Dave Posted February 15, 2006 Posted February 15, 2006 Well, use your brain a little. For a start, you can immediately disclude all of the even numbers since 2 divides any even number. There's millions of articles on this; just do a search on Wikipedia and you're bound to find something. For your information, the "great question" you were talking about is a formula for generating the n'th prime number. This is currently an unsolved problem.
Aeternus Posted February 15, 2006 Posted February 15, 2006 With regards to the palindrome check, if you simply convert the integer to a string (trivial, there'll be plenty of examples of this around, just using mod 10 and division to work out the digits and then adding a constant to put it in the right ascii range). Then you can do something like - int palindrome ( char *theString ) { unsigned int length = strlen( theString ) - 1; unsigned int counter = 0; int palindromeFlag = 1; while ( counter < length ) { if ( theString[counter] != theString[length] ) { palindromeFlag = 0; break; } length--; counter++; } return palindromeFlag; }; Obviously you would need to make sure when generating the string you null terminate it (ie this isnt a fully functioning stand alone code segment, you need a main() function and some IO stuff and conversion) but it might give you an idea of how to go about things. You might be able to do the same thing but without converting it into a string (ie by doing the checks for equality as you convert from the whole number to its digits without adding the additional constant to convert to ascii) but it was easier just to knock this one together quickly to give you an idea.
reng Posted February 15, 2006 Posted February 15, 2006 if it isn't a homework assignment i would be willing to help out, the easiest way (well the way i always use) is char* x = new char[10]; int the_number = 37; sprintf(x, "%d", the_number); as far as figuring out if the number is prime, it is easy enough to do if you follow the rules and don't mind speed.. obviously all numbers less than half the set to check, and if if it doesn't divide easily (look up the % operator, also remember to use a double and compare it to itself rounded to nearest 1).. this is all if you don't care about speed..
RyanJ Posted February 15, 2006 Posted February 15, 2006 Depends on how big the number would be... the bigger the harder it is too check (Extremly large numbers require the power of a supercomputer to check!) This may help for larger numbers: http://en.wikipedia.org/wiki/Prime_number#Primality_tests Cheers, Ryan Jones
fanatic Posted February 15, 2006 Posted February 15, 2006 This all very facinating. I have just recently started to study the C and C++ languages, but where do I get a good working compiler for free. I don't have hundreds to spend on simple compilers that microsoft enevitable charges a price that almost rivals the goss national product of some small countries.
RyanJ Posted February 15, 2006 Posted February 15, 2006 This all very facinating. I have just recently started to study the C and C++ languages, but where do I get a good working compiler for free. I don't have hundreds to spend on simple compilers that microsoft enevitable charges a price that almost rivals the goss national product of some small countries. http://bloodshed.net/dev/ Is the one I use, and its one of the best I think Cheers, Ryan Jones
reng Posted February 15, 2006 Posted February 15, 2006 Is the one I use' date=' and its one of the best I think [/quote'] A very good compiler indeed, I would have to stick with the nerds at borland (their turbo c/c++ compiler is free now). http://www.borland.com
RyanJ Posted February 15, 2006 Posted February 15, 2006 Gcc!!! GCC is more for the advanced user, Dev-C++ fits for basic too advanced (Features for all). I really reccomend Dev-C++ for basic users and maybe for advanced users but GCC may be more too their liking Cheers, Ryan Jones
timo Posted February 15, 2006 Posted February 15, 2006 Dev-C++ is not a compiler, it´s a developement enviroment. Actually, I thought Dev-C++ uses gcc as compiler but I´m not sure about that. EDIT: Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as its compiler. It creates native Win32 executables' date=' either console or GUI. Dev-C++ can also be used in combination with Cygwin. [/quote'] Now if I only knew what "Mingw port of GCC" is ...
RyanJ Posted February 15, 2006 Posted February 15, 2006 Dev-C++ is not a compiler' date=' it´s a developement enviroment. Actually, I thought Dev-C++ uses gcc as compiler but I´m not sure about that.[/quote'] Isn't that the point? Its better for basic users those who just started learning C / C++? The syntax highlighting also helps me find errors Cheers, Ryan Jones
Klaynos Posted February 15, 2006 Posted February 15, 2006 Isn't that the point? Its better for basic users those who just started learning C / C++? The syntax highlighting also helps me find errors Cheers' date=' Ryan Jones[/quote'] My votes always go for a text editor with syntax highlighting and a nice compiler like GCC... I'm for throwing people in the deep end, makes transitions in later life easier But that's just a personal thing...
RyanJ Posted February 15, 2006 Posted February 15, 2006 My votes always go for a text editor with syntax highlighting and a nice compiler like GCC... I'm for throwing people in the deep end, makes transitions in later life easier But that's just a personal thing... Thats the way I started out - I found it quite easy but from helping others learn I found that its best not to start that way for most peple - too much uts them off But if it works go for it! Cheers, Ryan Jones
Cap'n Refsmmat Posted February 15, 2006 Posted February 15, 2006 Dev-C++ is just an IDE for the Windows port of GCC.
reng Posted February 15, 2006 Posted February 15, 2006 Isn't that the point? Its better for basic users those who just started learning C / C++? The syntax highlighting also helps me find errors borland
grazzhoppa Posted February 16, 2006 Posted February 16, 2006 This all very facinating. I have just recently started to study the C and C++ languages, but where do I get a good working compiler for free. I don't have hundreds to spend on simple compilers that microsoft enevitable charges a price that almost rivals the goss national product of some small countries. MS Visual C++ express edition 2005 is free until Nov 2006. http://msdn.microsoft.com/vstudio/express/visualc/download/
timo Posted February 16, 2006 Posted February 16, 2006 MS Visual C++ express edition 2005 is free until Nov 2006. http://msdn.microsoft.com/vstudio/express/visualc/download/ I´m lazy so I´ll ask you instead of looking it up from the page you linked to: What does "it´s free until Novemver" mean? Does it mean "get it till November, afterwards you´ll have to pay for it" or does it mean "when you get it now, you can use it till November for free, afterwards you´ll have to register"?
Cloud Posted February 16, 2006 Posted February 16, 2006 Sorry to interfere - the discussion has already gone off topic anyway. I've been learning C++ (From http://www.cplusplus.com/doc/tutorial/) I'm about half-way through the tutorials. I'm using the Dev-c++ as the IDE. I was wondering what type of programs could I to make with the language. I was thinking of starting a password program. That's all I could think of. Could someone give me any hints as to other software I could potentially make with the C++ language. Thanks (understanding I'm still a newbie learning the language).
RyanJ Posted February 16, 2006 Posted February 16, 2006 I was wondering what type of programs could I to make with the language. I was thinking of starting a password program. That's all I could think of. Could someone give me any hints as to other software I could potentially make with the C++ language. C programs can do anything you need given sufficient knowledge, well almost but there are some things you can't do but would be of no concern to the average programmer and involve writing code in ASM. Pick something simple to start with and then develop it - you'll learn a lot and pick up ideas for other programs along the way Good luck! Ryan Jones
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now