File: ptest.cc

package info (click to toggle)
eclib 20190909-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,196 kB
  • sloc: cpp: 47,090; makefile: 251; sh: 122
file content (109 lines) | stat: -rw-r--r-- 3,848 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// ptest.cc -- test program for arith functions
//////////////////////////////////////////////////////////////////////////
//
// Copyright 1990-2012 John Cremona
// 
// This file is part of the eclib package.
// 
// eclib is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
// 
// eclib is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// 
// You should have received a copy of the GNU General Public License
// along with eclib; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
// 
//////////////////////////////////////////////////////////////////////////
 
#include <eclib/arith.h>
#include <functional> 

int main()
{
 long n, p;

 cout<<"Initialized table of " << nprimes() << " primes, up to "<<maxprime() << endl;

 cout<<"Enter an index: ";  cin >> n;
 p=prime_number(n);  cout << "prime_number("<<n<<") = "<<p<<endl;

 cout<<"How many primes do you want to see (as a vector<long>)? ";  cin >> n;
 vector<long> plist=primes(n);
 cout << plist << endl;

 cout<<"Enter a number to see if it is that list: "; cin>>p;
 vector<long>::iterator pi=find(plist.begin(),plist.end(),p);
 if(pi==plist.end()) cout<<"NOT in the list"<<endl;
 else    cout<<p<<" is list item "<<(pi-plist.begin())<<" (counting from 0)"<<endl;

 vector<long> v(10);
 iota(v.begin(),v.end(),1);
 cout<<"iota(10): "<<v<<endl;

 n=2310*210*64*17;
 cout<<"n = "<<n<<endl;
 vector<long> exps;

 transform(plist.begin(),plist.end(),inserter(exps,exps.end()),
	   bind2nd(ptr_fun(val),n));
 cout<<"exps = "<<exps<<endl;

 vector<long> plist1=primes(10);
 cout<<"Comparing previous prime list with "<<plist1<<endl;;
 cout<<"First starts with second, 10 items from 0: "<<startswith(plist,plist1,10)<<endl;
 cout<<"First starts with second,  5 items from 5: "<<startswith(plist,plist1,5,5)<<endl;
 cout<<"Second starts with first, 10 items from 0: "<<startswith(plist1,plist,10)<<endl;
 cout<<"Second starts with first,  5 items from 5: "<<startswith(plist1,plist,5,5)<<endl;

 //initialize a prime iterator for n primes
 cout<<"How many primes do you want to see (one by one)? ";  cin >> n;
 for(primevar pr(n); pr.ok(); pr++)
   cout << "Prime number " << pr.index() << " = " << pr << endl;
 
 long m;
 while (cout << "\nEnter an integer m (0 to stop): ", cin >> m, m!=0) 
   {
     cout << "Smallest prime factor of " << m << " is " << primdiv(m) << endl;
     plist=pdivs(m);
     cout << "m has " << plist.size() << " prime divisors: " << plist << endl;
     cout << "with exponents: "; 
     for(vector<long>::const_iterator pr = plist.begin(); pr!=plist.end(); pr++)
       cout << *pr <<":"<<val(*pr,m) << "\t";
     cout<<endl;

     vector<long> dlist=alldivs(m,plist);
     cout << "m has " << dlist.size() << " divisors: " << dlist << endl;
     dlist = posdivs(m,plist);
     cout << "m has " << dlist.size() 
          << " positive divisors: " << dlist << endl;
     dlist = sqdivs(m,plist);
     cout << "m has " << dlist.size() 
          << " positive divisors whose square divides m: " << dlist << endl;
     dlist = sqfreedivs(m,plist);
     cout << "m has " << dlist.size() 
          << " positive square-free divisors: " << dlist << endl;
 }

 long a,b;
 int cont=1;
 while(cont)
   {
     cout<<"Enter integers a b (0 0 to stop): ";
     cin>>a>>b;
     long g=gcd(a,b);
     if(g==0) cont=0;
     else
       {
	 cout<<"gcd = "<<g<<endl;
	 long l=lcm(a,b);
	 cout<<"lcm = "<<l<<endl;
       }
   }
 cout << endl;
}  /* main() */