Advertisement
Prohause

C# Advanced Lab - Algorithms

Jun 19th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. C# Advanced Lab - Algorithms
  2. This document defines algorithmic problems from the "Advanced C#" Course @ Software University. You are presented with some problems and certain steps you need to take in order to accomplish the tasks.
  3. Problem 1. Prime Factorization
  4. Fun fact: did you know int.MaxValue (231 – 1) is a prime?
  5. Prime factorization of a number N is the process of finding a set of prime numbers that multiply together to produce N. E.g. 12 can be represented as 2 * 2 * 3; 534543 = 3 * 23 * 61 * 127.
  6. There are useful online calculators you can use to check the prime factorization of a number, like this one.
  7. The task: Write a program that takes as input an integer number N (N >= 2) and represents it as a multiple of prime numbers in format: "[number] = [prime factor 1] * [prime factor 2] * … * [prime factor n]".
  8. Examples:
  9. Input Output
  10. 2 2 = 2
  11. 12 12 = 2 * 2 * 3
  12. 220 220 = 2 * 2 * 5 * 11
  13. 534543 534543 = 3 * 23 * 61 * 127
  14.  
  15. One possible approach:
  16. 1. Create a list to hold each prime multiple.
  17. 2. Set a variable divisor to 2 (the first prime number).
  18. 3. Check if N can be divided by divisor:
  19. a. If you can divide N by divisor without remainder, add divisor to the list and divide N by divisor. Repeat this step.
  20. b. If you cannot divide N by divisor without remainder, increment divisor and repeat step 3.
  21. 4. End the process when N equals 1.
  22. 5. Print the result in the specified format.
  23. Restrictions
  24. • The number N will always be a positive integer in the range [2 … 2 000 000 000]. There is no need to check it explicitly.
  25. • The prime factors of the number should be sorted in ascending order.
  26. • Allowed working time for your program: 0.9 seconds.
  27. • Allowed memory: 16 MB.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement