Check if number is prime [closed] - pascal

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can anybody please help me out with making Pascal function to check if number is prime? Boolean function returning true or false would be most helpful as my program requires looping through more numbers, testing them out and outputting if they are indeed prime. This is the part I can manage but I'm having problems with constructing the actual function to check it.
Thanks a ton

Try this:
Program Primechk;
Var
Num : Integer;
checker,count,adder : ShortInt;
Begin
Write('Enter one number : ');
Readln(Num);
adder := 0;
For count := 1 to 10 do
begin
checker := num mod count;
if checker = 0 then
adder := adder + 1
end;
if (num <= 10) and (adder > 2) then
Writeln(num, ' is not a prime number')
else
if (num > 10) and (adder > 1) then
Writeln(num, ' is not a prime number')
else
Writeln(num,' is a prime number');
end.
You can use it as a base and modifie it to your needs.
Simple googleing :]

Related

My program won't print out correct math numbers

I'm coding a little program but it won't print out the right math answer, it just stays at 993.
The Code:
program _Gul_;
uses crt;
var a: integer;
var b: integer;
begin
writeln('1000 - 7?');
a := 1000;
b := a - 7;
while a > 0 do
begin
writeln (a - 7, ' - 7?');
delay(120);
a := a - 7;
writeln (b)
if a = 6 then
break;
end;
writeln('я гуль')
end
I don't quite know why it is not working. I defined "b" and made a command that prints it out and the output is just:
You never update the value in b. In point of fact, b is not necessary to your program at all. Your printing strategy is also more complicated than it needs to be. Print a minus 7, then do the subtraction and print it. This prevents the program telling you the rest of 6 - 7 is 6.
program _Gul_;
uses
crt;
var
a: integer;
begin
a := 1000;
while a > 0 do
begin
writeln (a, ' - 7?');
delay(120);
a := a - 7;
writeln (a);
if a = 6 then
break;
end;
writeln('я гуль')
end.

Replace consecutive numbers with mathematical form [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am working on compression algorithm for which I want to replace all consecutive number with its mathematical form which is not logical mathematically but my algorithm will know and convert it to original form.
Suppose I have string:
string input = "732183900000000000002389288888888888888";
Did you see it have 0000000000 and 8888888888888 are major consecutive duplicates.
And now I want to convert those to:
//convert 000000000 to 0*9. Means 9 times 0.
//convert 888888888 to 8*9. Means 8 times 0.
string output = "7321839" +
"0*13" +
"23892" +
"8*14";
//or
string output = "7321839-0*13-23892-8*14";
Points to consider:
Any language that works on windows will be accepted. For me main thing is algorithm.
Please keep performance in mind as it would be used for big files.
To be honest this is as simple as it gets:
Parse through the string one character at a time.
Check if the previous character is the same as the current one.
If it is same then increment a counter variable or else reset it to 0.
If the counter value is greater than one when we reset the counter to 0 then add * to the result.
Regex might be a bit convoluted for this given the rules for dashes (although not impossible by any means),
Seemingly, you want the following
Groups of the same number greater than the count of 1
No prefix dash
No suffix dash
No double dashes (speculative)
Here is a fairly efficient C# O(n) implementation with StringBuilder, which inurn should allow you to work with exceedingly large strings with minimal allocations
Given
public static string Shorten(string value)
{
var sb = new StringBuilder(value.Length);
int i, last;
var isLastGroup = false;
void Write()
{
var isGroup = i - last > 1;
var getDash = last == 0 || isLastGroup ? "" : "-";
sb.Append(isGroup ? $"{getDash}{value[last]}*{i - last}{(i != value.Length ? "-" : "")}" : value[last].ToString());
isLastGroup = isGroup;
last = i;
}
for (i = 0, last = 0; i < value.Length; i++)
if (value[last] != value[i])
Write();
Write();
return sb.ToString();
}
Tests
Console.WriteLine(Shorten("1"));
Console.WriteLine(Shorten("111"));
Console.WriteLine(Shorten("1112"));
Console.WriteLine(Shorten("1222"));
Console.WriteLine(Shorten("12233344445555512345"));
Results
1
13
13-2
1-23
1-22-33-44-5*5-12345
Full Demo Here

Draw n random integers whose sum is equal to 100 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Pseudocode example:
Random function: (1 to 5, values less than 100, sum must be equal to 100 when all random numbers are added).
Result example:
Number 1 = 35
Number 2 = 15
Number 3 = 10
Number 4 = 20
Number 5 = 20
Your problem isn't quite well-defined. There are many possible solutions, with different properties.
Here is the first one that sprung to my mind: create a subdivision of the interval [0, 1] into n parts by choosing n − 1 points in [0, 1] randomly. Then scale this to [0, A] and use the lengths of these subintervals as your n random numbers with sum A.
function GetRandomNumbers(ACount: Integer; const ASum: Double): TArray<Double>;
var
Itvs: TArray<Double>;
i: Integer;
begin
if ACount < 1 then
raise Exception.Create('GetRandomNumbers: Invalid parameters.');
// Create a subdivision of [0, 1]
SetLength(Itvs, ACount + 1);
Itvs[0] := 0;
for i := 1 to ACount - 1 do
Itvs[i] := Random;
Itvs[ACount] := 1;
TArray.Sort<Double>(Itvs);
SetLength(Result, ACount);
for i := 0 to ACount - 1 do
Result[i] := ASum * (Itvs[i + 1] - Itvs[i]);
end;
For example, this might give
16.7746451916173
7.29391833301634
22.1434036735445
3.25182809028775
50.5362047115341
for n = 5 and A = 100.
This uses modern Delphi techniques like generics, but the general idea should be clear enough, so you can implement it in Delphi 7 and use any sorting method you like. Also, I'll leave it as an exercise to make an integer version of GetRandomNumbers.
Using only integer numbers and Fisher-Yates shuffle:
program cont3;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
const
SummandsCount = 5;
WantedSum = 100;
var
i, j, t, Cnt, WhereToInsert: integer;
JustNaturalNumbers: array[1..WantedSum] of Integer;
DividingPoints: array[0..SummandsCount] of Integer;
begin
Randomize;
Cnt := 1;
DividingPoints[0] := 0;
DividingPoints[SummandsCount] := 100;
for i := 1 to WantedSum - 1 do
JustNaturalNumbers[i] := i;
for i := WantedSum - 1 downto WantedSum - SummandsCount + 1 do begin
j := 1 + Random(i);
WhereToInsert := Cnt;
while (WhereToInsert > 1) and (JustNaturalNumbers[j] < DividingPoints[WhereToInsert-1]) do begin
Dec(WhereToInsert);
DividingPoints[WhereToInsert + 1] := DividingPoints[WhereToInsert]
end;
DividingPoints[WhereToInsert] := JustNaturalNumbers[j];
JustNaturalNumbers[j] := JustNaturalNumbers[i];
Inc(Cnt);
end;
t := 0;
for i := 1 to SummandsCount do begin
Writeln(DividingPoints[i] - DividingPoints[i-1]);
t := t + (DividingPoints[i] - DividingPoints[i-1]);
end;
Writeln('Sum = ', t);
Readln;
end.
Output example:
22
4
7
18
49
Sum = 100

A Boolean is required [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I want to change the background color of Object in Crystal report according to some Condition which is (e.g)
if (({DataTableCable.ColumnChange} and 4) = 4) then
crGreen
DataTableCable.ColumnChange is a field Contains a number , i want to make a bitwise and with 4 , if the result is 4 then change the background of Object
but i receive the following error
"A Boolean is required"
how should make a bitwise and in crystal report?
Your code doesn't compile in C# or VB.NET at all. However, maybe you want something like this:
if (DataTableCable.ColumnChange == 4)
{
// change to green
}
i want to check if the logical and of ColumnChange with 4 is 4 then do the rest
so you need to use & < logical and, and use == (comparision) instead of = (assignment)
if ((DataTableCable.ColumnChange & 4)== 4)
{
// change to green
}
So, if I'm reading your post correctly, you are trying to perform a bitwise comparison against your value ({DataTableCable.ColumnChange}) in the Crystal Reports code.
Looking around online, I can't seem to see anything that indicates that crystal can perform that kind of comparison. One alternative is to perform that calculation in your .Net code, and store it against a property/column of your datasource object - that way you can simply check if the property is true/false.
Update
Another option, perhaps, is to perform manual arithmetic to calculate this. Nopadol (Link provided by OP) seems to have good examples of how to do this (I've reproduced the code below for posterity). The No Stress Tech Guide to Crystal Reports Basic for Visual Studio 2008 (Lesson 9) seems to cover the use of the Formula Editor which should help get you started.
Function Name: ToBin
Function (NumberVar n)
(
Local StringVar b;
Local NumberVar d;
b := "";
d := n;
Do
(
b := CStr(Truncate(d Mod 2),0) & b;
d := Truncate(d / 2);
)
While (d > 1);
CStr(Truncate(d),0) & b;
)
Function Name: ToNum
Function (StringVar b)
(
Local NumberVar lng := length(b);
Local NumberVar n := 0;
Local NumberVar i;
For i := lng To 1 Step -1 Do
(
n := n + ((2 ^ (lng – i)) * Truncate(ToNumber(Mid(b,i,1))))
);
n;
)
Function Name: BitAnd
Function (NumberVar n1, NumberVar n2)
(
Local StringVar b1 := ToBin(n1);
Local StringVar b2 := ToBin(n2);
Local NumberVar l1 := length(b1);
Local NumberVar l2 := length(b2);
Local NumberVar lng := IIF(l1 > l2, l1, l2);
Local StringVar x;
Local StringVar y;
Local StringVar z := "";
Local NumberVar i;
For i := lng To 1 Step -1 Do
(
x := "0";
If (l1 >= i) Then
x := Mid(b1, (l1-i)+1, 1);
y := "0";
If (l2 >= i) Then
y := Mid(b2, (l2-i)+1, 1);
z := z & IIF(x=y And x="1","1","0");
);
ToNum(z);
)
Function Name: BitXOr
Function (NumberVar n1, NumberVar n2)
(
n1 + n2 – (BitAnd(n1, n2) * 2)
)
Function Name: BitOr
Function (NumberVar n1, NumberVar n2)
(
n1 + n2 – BitAnd(n1, n2)
)
Example
BitAnd(1, 5) // = 1
BitXOr(1, 5) // = 4
BitOr(1, 5) // = 5

Derangement or what? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
I was looking at the description of a derangement that states "a derangement is a permutation of the elements of a set such that none of the elements appear in their original position". But then it gave 9 derangements for a set of 4 items. That doesn't make sense to me, because I only get 4 discrete sets from 4 items.
For example:
1234
3142
2413
4321
Is there a different term than derangement for sets where the numbers don't have the same order as in any other set, based on a particular number of items?
And does anyone know of an algorithm for generating the derangements?
The nine derangements are:
2143
2341
2413
3142
3412
3421
4123
4312
4321
As you can see, column 1 does not contain 1, column 2 does not contain 2 and so on. In addition, every row has the numbers 1, 2, 3 and 4 and there are no duplicates (they're sorted to make that easier to detect).
For what it's worth, that was discovered with a brute force attack (the attack space was relatively small):
#include <stdio.h>
int main (void) {
int a, b, c, d, skip;
for (a = 1; a <= 4; a++) {
for (b = 1; b <= 4; b++) {
for (c = 1; c <= 4; c++) {
for (d = 1; d <= 4; d++) {
skip = (a==1) || (b==2) || (c==3) || (d==4);
skip |= (a==b) || (a==c) || (a==d);
skip |= (b==c) || (b==d);
skip |= (c==d);
if (!skip) {
printf ("%d%d%d%d\n", a, b, c, d);
}
}
}
}
}
return 0;
}
It turned out that I was looking for a complete latin square (row-complete and or column-complete). I had already coded an algorithm to detect those, but didn't know if this kind of thing had a name. That's it, thanks.

Resources