Advertisement
rawyon

Untitled

Mar 30th, 2013
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. bool perfect_sqr( unsigned long long );
  7. bool prime ( unsigned long long);
  8. void print( unsigned long long, unsigned );
  9. int main()
  10. {
  11.   int t, j;
  12.   bool repeat = true;
  13.   unsigned long long triangle = 0, multipel, x = 0;
  14.   unsigned counter = 0;
  15.   cout << "Skriv in ett kravet på minst antal delare!\n";
  16.   cin >> t;
  17.   cout << endl;
  18.   while( counter <= t)
  19.     {
  20.       triangle += ++x;
  21.       counter  = 2; // triangle
  22.  
  23.       for (unsigned long i = 2 ; i <= triangle/2; ++i )
  24.     {
  25.       j = 2;
  26.       if( prime(i) )
  27.         {
  28.           multipel = i;
  29.           while( triangle % multipel == 0 )
  30.         {
  31.           counter++;     
  32.           while( repeat )
  33.             {
  34.               repeat = false;
  35.               for( unsigned c = 2; c < multipel/2; ++c )
  36.             {
  37.               multipel = i;
  38.               multipel *= j++;
  39.               if( multipel % c == 0 )
  40.                 repeat = true;
  41.             }
  42.             }
  43.         }
  44.         }      
  45.     }
  46.     }
  47.   print(triangle, t);  
  48.  
  49.   return 0;
  50. }
  51. bool prime ( unsigned long long n )
  52. {
  53.   for( unsigned i = 2; i < sqrt(n); ++i )
  54.     {
  55.       if ( n % i == 0 )
  56.     return false;
  57.     }
  58.   return true;
  59. }
  60.  
  61. void print( unsigned long long triangle, unsigned counter )
  62. {
  63.   cout << "Det första talet som har minst " << counter << " delare är " << triangle << endl;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement