Reply To: Programming
The mystery of the silver bullet › Forums › The Intelligence Room › Programming › Reply To: Programming
/*
MY CODE TO MY FIRST 3 QUESTIONS
================================
For what it is worth, good or bad, here is the code I wrote using PARI-gp.
I have not included the code’s print out for Q#3 as every one who posted code and answer
gave the correct answer, well done y’all!
I very rarely use python (a wonderful and most popular language these days) and the
code given in the forum by participants has been very helpful, I hope it has for others too.
Some of you are really knowledgeable and clever!
*/
\\ ======================================================== Question #1
\\ FIND X
\\ ===================================================== PARI-gp CODE
{for(x=5000,50000,
a=x%4;b=x%5;c=x%7;d=x%11;e=x%17;
if(a==3&&b==1&&c==2&&d==2&&e==12,print(“\n\\\\ Answer: “n)));}
\\ Answer: 29031
\\ ======================================================== Question #2
\\ FIND N AND PRIMES
\\ =================================================== PARI-gp CODE
\\ CODE TO FIND THE NUMBER
\\ Try all 4 digit numbers
for(i=1000,9999,d=divisors(i);if(#d==60,print(“\\\\ “i)));
\\ 5040
\\ 7920
\\ 8400
\\ 9360
\\ It is the first 4 digit number 5040 having 60 divisors that we seek
\\ (There are 3 more 4 digit numbers with 60 divisors but with wrong prime totals)
\\ CODE TO FIND THE 42 CONSECUTIVE PRIMES THAT ADD TO THE NUMBER 5040
{true=1;seed=2;
while(true,
total=0;
forprime(p=seed,1000,total+=p;
p1=nextprime(seed);
if(total==5040,print1(“\n\\\\ Answer: The consecutive primes “p1” to “p” sum to “total);true=0;break);
);
seed=p1+1;
);}
\\ Answer: The consecutive primes 23 to 229 sum to 5040
\\ CODE TO PRINT THE 42 PRIMES:
total=0;print1(“\\\\ “);forprime(p=23,230,total+=p;print1(p” “);if(total>=5040,break));
\\ 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229
\\ OR IF THE START AND END PRIME IS KNOWN:
count=0;print1(“\\\\ “);forprime(p=23,229,count++;print1(p” “);if(count==42,break));
\\ 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229
\\ ======================================================== Question #3
\\ WAYS WITH MONEY
\\ ===================================================== PARI-gp Code
\\ By the way gp ignores all white space/tabs in the code
\\ the following code is for friendly visual layout only
\\ ***USER FUNCTION*** prints only the 1st 10 and last 10 sets.
CountTheCoins()={
print(“\n\\\\ ===================== PROGRAM OUTPUT ======================”);
print(“\n\\\\ Please wait for the last 10 to print out\n”);
print(“\\\\\tset#)\tcoin count of(coin value)”);
my(purse,count=0);
for(penny=0,100, \\ 100/1
for(tuppence= 0,50, \\ 100/2
for(fivepence= 0,20, \\ 100/5
for(tenpence= 0,10, \\ 100/10
for(twentypence= 0,5, \\ 100/20
for(fiftypence= 0,2,\\ 100/50
for(pound= 0,1, \\ 100/100
purse = (penny + tuppence*2 + fivepence*5 + tenpence*10 + twentypence*20 + fiftypence*50 + pound*100);
if(purse==100,
if(count<10||count>=4553,\\ pre computed (4563-10)
print(“\\\\\t”,count+1,”)\t”,penny,”(1p) “,tuppence,”(2p) “,fivepence,”(5p) “,tenpence,”(10p) “,
twentypence,”(20p) “,fiftypence,”(50p) “,pound,”(100p) “);
);count++;
)
)
)
)
)
)
)
);
print(“\n\\\\ The number of possible ways (sets) is: “, count);}
\\ ***Call the function***
CountTheCoins();
\\
/*
===============================================================================
You can safely copy ALL of this post and paste it but you will see some duplicated answers
If you want you can copy and paste the gp code into an online compiler and
execute it to get/see output.
===============================================================================
*/