C++ program running too slow

vickybat

I am the night...I am...
nope, :( still not running.. ideone says time limit exceeded while python 2.6 on my computer does not produce any output even after few minutes.
OKAY it took 15 minutes on my PC to complete the code(not sure if my PC is slow or the code). . mate, companies will kick me out for compiling this question in 15 minutes. Let's try a better algorithm

Yup its taking a lot of time in python. My earlier Java code takes just over a second to complete. Dunno why the same logic is so slow in python.
Try copy pasting my java code in an eclipse or netbeans ide. It will generate output in just over a second. Divisibility checks are the slowest form of computation.

That said, chaitanya's code is extremely optimised. That's because he uses the sieve of eratosthenes algorithm to generate primes and stores them in an array.
The SOE algorithm does not has any form of divisibility check to determine primes. It relies on striking out the multiples of prime numbers in each iteration and the numbers left out are primes.No divisibilty check per iterartion here.

Then he compares the highest power of each prime with the largest range, then multiplies them in each iteration. So the LCM of all these numbers gives the resultant output.

I'm trying a simplified boolean function in java that does the same thing but much simplified. Didn't quite get the time today, but will post soon as its complete.

Chaitanya's method is highly optimized and even OP should consider this in C++. :)

P.S= What are your PC specs??
 
Last edited:

Chaitanya

Cyborg Agent
Yup its taking a lot of time in python. My earlier Java code takes just over a second to complete. Dunno why the same logic is so slow in python.
Try copy pasting my java code in an eclipse or netbeans ide. It will generate output in just over a second.

Noob question which one is java code & which one is python?? :-D
How to compile python ??

somewhere read that although python is simple & easy but is slower as compared to C/C++ (dunno abt it's authenticity)
 

vickybat

I am the night...I am...
Noob question which one is java code & which one is python?? :-D
How to compile python ??

somewhere read that although python is simple & easy but is slower as compared to C/C++ (dunno abt it's authenticity)

Post # 8 - Java
Post # 10 -python

:)

Well in java its fast enough but taking a lot slower in python. Couldn't figure out why though.
Python is an interpreter based and dynamically typed language. Its not compiled as a whole but interpreted line by line at runtime, just like javascript, where browsers are interpreters.

Python is simple and easy but i kind of dislike the indentations. One has to be very careful there. That said, both java and python have different usage.
For example, python is used to build web crawlers ( search engines) and kind of forms the building block.

String methods are highly efficient in python and has the easiest implementation. Pattern matching and usage of regular expressions is ideal in python, which are used in several validations in search engines and several other applications like email clients. Every language has a different usage pattern and has their drawbacks in certain scenarios. The python code here will be a solid example. :)

That said, python is as authentic as c or c++. Coding a search engine in c++ is extremely tedious. Python is used for its simplicity in certain scenarios. Similarly game engines are build mostly in C++ where more low level control is required.

I'm not a programmer but have recently developed great interest in it and open to learn new things. I'm an electronics and telecomm graduate btw. Never had any experience in programming during college, but loving it now. :))
 

Chaitanya

Cyborg Agent
I'm not a programmer but have recently developed great interest in it and open to learn new things. I'm an electronics and telecomm graduate btw. Never had any experience in programming during college, but loving it now. :))

Lovely info.. :)
Mee too.. I'm not from CS/IT but have keen interest in prog. will learn Python & Java altho python would be of greater preference.. :-D
Any tips/guides. Any good study material ?

String methods are highly efficient in python and has the easiest implementation. Pattern matching and usage of regular expressions is ideal in python, which are used in several validations in search engines and several other applications like email clients. Every language has a different usage pattern and has their drawbacks in certain scenarios. The python code here will be a solid example. :)

That said, python is as authentic as c or c++. Coding a search engine in c++ is extremely tedious. Python is used for its simplicity in certain scenarios. Similarly game engines are build mostly in C++ where more low level control is required.

I was planning such hard $hit on c++ ..
But seems python has paved a way out for me
 

vickybat

I am the night...I am...
Lovely info.. :)
Mee too.. I'm not from CS/IT but have keen interest in prog. will learn Python & Java altho python would be of greater preference.. :-D
Any tips/guides. Any good study material ?



I was planning such hard $hit on c++ ..
But seems python has paved a way out for me

If you want to learn python, then join here:


*www.udacity.com/

Take the "intro to computer science" class. Its completely about python. Btw, i'm almost halfway now. :))
 

rohitshubham

Ambassador of Buzz
Yup its taking a lot of time in python. My earlier Java code takes just over a second to complete. Dunno why the same logic is so slow in python.
Try copy pasting my java code in an eclipse or netbeans ide. It will generate output in just over a second. Divisibility checks are the slowest form of computation.

That said, chaitanya's code is extremely optimised. That's because he uses the sieve of eratosthenes algorithm to generate primes and stores them in an array.
The SOE algorithm does not has any form of divisibility check to determine primes. It relies on striking out the multiples of prime numbers in each iteration and the numbers left out are primes.No divisibilty check per iterartion here.

Then he compares the highest power of each prime with the largest range, then multiplies them in each iteration. So the LCM of all these numbers gives the resultant output.

I'm trying a simplified boolean function in java that does the same thing but much simplified. Didn't quite get the time today, but will post soon as its complete.

Chaitanya's method is highly optimized and even OP should consider this in C++. :)

P.S= What are your PC specs??
it's ancient :0
like P4 and stuff
even triend it on my old lappy and it gave results in 10 min . its AMD m3000+ 64 bit dual core, it was working for 50%cpu constantly
 

vickybat

I am the night...I am...
it's ancient :0
like P4 and stuff
even triend it on my old lappy and it gave results in 10 min . its AMD m3000+ 64 bit dual core, it was working for 50%cpu constantly

These are the culprits then. I have a quad core i5 750. :))
Anyways, run chaitanya's code in your machine and post back here about its execution times. :)
 

Chaitanya

Cyborg Agent
^Yeah its c++ code.
Which compiler are you using??
borland/turbo c++ are bit too archaic for the code.
 

nisargshah95

Your Ad here
I brute forced and checked if the number (starting with 19*20=380, wild guess) it was divisible by 11 through 18 and incremented it by 19*20=380. Got my answer under 1 sec. in Python.
 

rohitshubham

Ambassador of Buzz
use Code::blocks with mingw
i am using ideone but the results are not correct. TDF.JPG

use Code::blocks with mingw
i am using ideone but the results are not correct.TDF.JPG
 

Chaitanya

Cyborg Agent
Smaller ??
try Devc++ ... It's old & no current support for it + doesn't work on windows 8

But is good enough for the job. ;-)
 
Top Bottom