473,382 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Can somebody tell me the efficiency of the following:

1. Factorial program using
a. Iteration
b. Recursion
2.
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 3; i++) { //linear
  2.             for(int j = 0; j < 4; j ++){ // log (n)
  3.                     //do constant time stuff
  4.             }
  5.     }
Will it be O(12) or something else?

3.
Expand|Select|Wrap|Line Numbers
  1. for(int j = 0; j < n; j *= 2){ 
  2.             //do constant time stuff
  3.     }
and
Expand|Select|Wrap|Line Numbers
  1. for(int j = 0; j < n; j *= 10){ 
  2.             //do constant time stuff
  3.     }
Does the big-O automatically become O(logN)?

5.
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < n*n; i++) {
  2.             cout << i << endl;
  3.     }
Will it be O(n^n) or O(n) only.
Sep 2 '10 #1
4 2286
Oralloy
987 Expert 512MB
Shilpa,

We won't do your homework for you.

Think about the answers, write what you think, then ask. That's how this site works. Don't worry, you'll get lots of help.

Cheers!

p.s. #1 is tricky, the rest are trivial. Think about how a Turing machine works. Then, think about techniques for computing mathematic values. Be sure to state your assumptions.

:)
Sep 2 '10 #2
Hi Oralloy,

Thanks for your prompt reply. However neither I am a student nor is this any of my homework exercise.

I was reading articles on calculating efficiency and some doubts arose in from there. I am little confused, so have even given the options with them.

I am new to calculating efficiency, so please request you to help me out in clearing my doubts.

Thanks,
Shilpa.
Sep 3 '10 #3
Nepomuk
3,112 Expert 2GB
Hi Shilpa!

As every student could claim to not be one, we have no way of knowing if you are one or not. However, I'll give you a few tips that I would also feel comfortable giving you if you were a student.

The big O notation (or Landau notation) only gives rough estimates. In general, f in O(x) with x being dependent on n means that there's a real number a so that f takes a maximum of a*x steps. It doesn't matter if a is 2 or 1000000, that's the same in big O notation. Oh, and you normally take the minimum x for which this is true. So you could write O(2n) and then a may be 3, but you could just as well write O(n) with a=6, the latter being the generally used form.

A tip about the first example: Think about, how you would implement the calculation of n! through iteration and recursion. How many steps would it take with either approach?

Oh by the way, and I'm assuming you're talking about time efficiency, not space efficiency? The Big O notation works for both, but your answers suggest time and that is often the one you're more likely to talk about when learning about algorithms.

Greetings,
Nepomuk
Sep 3 '10 #4
jkmyoung
2,057 Expert 2GB
1. This really depends on if you're counting the number of bit operations, or using a larger abstract datatype like a int16 or int64.

Further, there are many different ways to implement this operation. eg one example could include the following (tries to reduce number of bit multiplications): Find 10!

2*3 = 6 (store on stack)
4*5 = 20 (look at stack, see 6 is lower)
20 * 6 = 120 (store on stack)
6 * 7 = 42 (look at stack, see 120 is bigger, store on stack)
8 * 9 = 72 (look at stack, see 42 is lower)
42 * 72 = 3024 (look at stack, see 120 is lower)
3024 * 120 = 362880
362880 * 10 = 3628800
===
Assuming the basic method,
basic runtime assuming a large enough integer datatype is O(n). However, recursion will take longer due to the function overhead in each recursive step.

2. O(1)

3. Yes.
O (log2(n)) is equivalent to O(log10(n)) is equivalent to O(log(n))


5. Try some examples. eg n = 1, 2, 3, 4, 5, 10, 50, 100, and see your results.
Sep 7 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: John Dean | last post by:
Hi I am in the process of porting Rekall to Mac X OS and I am having trouble finding the Python SDK. I would be grateful if somebody would be kind enough to point me in the right direction ...
38
by: aaronfude | last post by:
I'm working on a scientific computing application. I need a class called Element which is no more than a collection of integers, or "nodes" and has only on method int getNode(int i). I would...
9
by: travisperkins03 | last post by:
Hi, I have read somewhere that C code sometimes cannot be compiled to be as efficient as FORTRAN, eg for matrix multiplication, because a C compiler cannot make the assumptions about arrays that...
6
by: Eddy | last post by:
Hi ! I have to help a student with his school project. He is learning 'C', using Turbo C 2.1 on a Windows XP pc. ( don't ask me why pls ! ) I proposed him to decode a DCF77 time signal...
10
by: Russ Brown | last post by:
Hello all, Recently a post on this list made me think a bit about the way in which I write my queries. I have always written queries with ordinary joins in this manner: SELECT * FROM a, b...
6
by: Kenny McCormack | last post by:
between printf("hello, world\n"); and printf("goodbye, universe\n"); ?
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
5
by: Adisco | last post by:
Hello, I'm Adisco. Can somebody tell me how to start, because I want to learn Visual Basic. I'm a student in Sierra Leone studying Electrical and Electronics Engineering. I'm really interested in IT...
1
by: abhishekgvyas | last post by:
Hi, can somebody tell me what is wrong with the following stored procedure code.. SQL> CREATE OR REPLACE PROCEDURE DEBIT_ACCOUNT(ACCT_ID IN NUMBER, DEBIT_AMOUNT OUT NUMBER) 2 IS 3 ...
3
by: Alpha83 | last post by:
Hi, Is there a code measuring tool that tells you which is more efficient cost-wise. For example, if I were to compare the following two identical code blocks, how do I know, which is more...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.