Advertisement
Guest User

SoftUni_Homework_1_1&2

a guest
Aug 17th, 2015
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SoftUni_HomeWork_1_2
  7. {
  8. class Program
  9. {
  10. const uint ARRAY_SIZE = 2;
  11.  
  12. /// <summary>
  13. /// Check if a given number is a fibonacci number and returns its index in the sequence
  14. /// </summary>
  15. /// <param name="number">Number to be checked</param>
  16. /// <returns>-1 if the number is not part of the fibonacci sequence or the index of the number in the sequence</returns>
  17. static int Fibonacci(uint number)
  18. {
  19. //Uses a closed form solution for the fibonacci number calculation.
  20. //http://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression
  21. double fi = (1 + Math.Sqrt(5)) / 2.0; //Golden ratio
  22. int n = (int)Math.Floor(Math.Log(number * Math.Sqrt(5) + 0.5, fi)); //Find's the index (n) of the given number in the fibonacci sequence
  23. int actualFibonacciNumber = (int)Math.Floor(Math.Pow(fi, n) / Math.Sqrt(5) + 0.5); //Finds the actual number corresponding to given index (n)
  24. if (actualFibonacciNumber == number)
  25. {
  26. return n;
  27. }
  28. else
  29. {
  30. return -1;
  31. }
  32. }
  33.  
  34. /// <summary>
  35. /// This method tests if a given is Prime. Function is not optimized!
  36. /// </summary>
  37. /// <param name="number"> The number to be checked</param>
  38. /// <returns></returns>
  39. static bool PrimeCheck(uint number)
  40. {
  41. // Get the divisors smaller than the square root of the number
  42. uint divisors = (uint)Math.Sqrt(number);
  43.  
  44. // Check if the number can be devided by any of the integer numbers smaller than its square root
  45. for (int cnt = 2; cnt <= divisors; cnt++)
  46. {
  47. if (number % cnt == 0)
  48. {
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54.  
  55. static void Main(string[] args)
  56. {
  57.  
  58. uint number = 1; // Use it to store the current value
  59.  
  60. uint[] number_of_primes = new uint[] { 24, 101, 251 }; // This array stores the numbers of the primes asked in the task
  61.  
  62. uint counter = 1; // Counter variable used to itterate in the loop
  63.  
  64. while (counter <= number_of_primes[ARRAY_SIZE])
  65. {
  66. if (Program.PrimeCheck(++number)) // Increment the number and check if it's a prime number
  67. {
  68. if (number_of_primes.Contains(counter)) // Output the value of the prime number if it's contained in the array
  69. {
  70. Console.Write(counter.ToString() + ": " + number.ToString());
  71. int index = Fibonacci(number); // Check if the number is part of the fibonacci sequence
  72. if (index != -1)
  73. {
  74. Console.Write(" is a fibonacci number with index: " + index.ToString() + '\n');
  75. }
  76. else
  77. {
  78. Console.Write('\n');
  79. }
  80. }
  81. counter++;
  82. }
  83. }
  84. Console.ReadKey();
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement