[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Convert vbscript function to C#

Posted on 2012-03-30
4
Medium Priority
?
479 Views
Last Modified: 2012-04-04
I need to convert the following vb script function to C# to be used in a new application.
Function EX(ByRef x, p, m)
    y = 1
    Do While p > 0
        Do While (p / 2) = Int((p / 2))
            x = (x * x) - (Int((x * x) / m) * m)
            p = p / 2
        Loop
        y = (x * y) - (Int((x * y) / m) * m)
        p = p - 1
    Loop
    EX = y
End Function

Open in new window


This is my attempt, but it does not work correctly. The original returns y as a number less than 1000 and my conversion returns numbers in the millions and runs many more iterations of the inner loop.

       public int Ex(ref int x, int p, int m)
        {
            y = 1;
            do
            {
                do
                {
                    x = (x * x) - (((x * x) / m) * m);
                    p = p / 2;
                }
                while ((p % 2) == 0);
                y = (x * y) - (((x * y) / m) * m);
                p = p - 1;
            }
            while (p > 0);
            return y;
        }

Open in new window


I have attached another version of the vbscript function with logging statements and the logs it generated so you can see the data involved since they used no datatypes when it was written.

Function EX(ByRef x, p, m)
	logmessage "gis.asp: EX", "	Variables passed in: x = " & x & " p = " & p & " m = " & m
    y = 1
    i =1
    o =1
    Do While p > 0
        Do While (p / 2) = Int((p / 2))
            x = (x * x) - (Int((x * x) / m) * m)
            logmessage "gis.asp: EX", "		(x * x)" & (x * x) & " - (Int((x * x) / m)" & Int((x * x) / m) & " * m)" & (Int((x * x) / m) * m) & " = " & (x * x) - (Int((x * x) / m) * m)
            p = p / 2
            logmessage "gis.asp: EX", "		inner iteration"& i &": p/2 = " & p/2 & " p = " & p & " y = " & y & " x = " & x
            i = i + 1
        Loop
			logmessage "gis.asp: EX", "	post inner do: p = " & p & " y = " & y & " x = " & x
			i=1
        y = (x * y) - (Int((x * y) / m) * m)
        p = p - 1
            logmessage "gis.asp: EX", "	outer iteration"& o &": p = " & p & " y = " & y & " x = " & x
            o = o + 1
    Loop
    o=1
        logmessage "gis.asp: EX", "post outer do: p = " & p & " y = " & y & " x = " & x
    EX = y
End Function


3/30/2012 9:26:46 AM	gis.asp: EX		Variables passed in: x = 15344606 p = 18346651 m = 22833299
3/30/2012 9:26:46 AM	gis.asp: EX		post inner do: p = 18346651 y = 1 x = 15344606
3/30/2012 9:26:46 AM	gis.asp: EX		outer iteration1: p = 18346650 y = 15344606 x = 15344606
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)506571035879689 - (Int((x * x) / m)22185626 * m)506571031960174 = 3919515
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration1: p/2 = 4586662.5 p = 9173325 y = 15344606 x = 22507133
3/30/2012 9:26:46 AM	gis.asp: EX		post inner do: p = 9173325 y = 15344606 x = 22507133
3/30/2012 9:26:46 AM	gis.asp: EX		outer iteration2: p = 9173324 y = 10547111 x = 22507133
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)15362597835225 - (Int((x * x) / m)672815 * m)15362586066685 = 11768540
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration1: p/2 = 2293331 p = 4586662 y = 10547111 x = 3919515
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)138498533731600 - (Int((x * x) / m)6065638 * m)138498526079762 = 7651838
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration2: p/2 = 1146665.5 p = 2293331 y = 10547111 x = 11768540
3/30/2012 9:26:46 AM	gis.asp: EX		post inner do: p = 2293331 y = 10547111 x = 11768540
3/30/2012 9:26:46 AM	gis.asp: EX		outer iteration3: p = 2293330 y = 994040 x = 11768540
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)58550624778244 - (Int((x * x) / m)2564264 * m)58550606626936 = 18151308
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration1: p/2 = 573332.5 p = 1146665 y = 994040 x = 7651838
3/30/2012 9:26:46 AM	gis.asp: EX		post inner do: p = 1146665 y = 994040 x = 7651838
3/30/2012 9:26:46 AM	gis.asp: EX		outer iteration4: p = 1146664 y = 4482640 x = 7651838
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)329469982110864 - (Int((x * x) / m)14429363 * m)329469959758537 = 22352327
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration1: p/2 = 286666 p = 573332 y = 4482640 x = 18151308
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)499626522314929 - (Int((x * x) / m)21881486 * m)499626512402314 = 9912615
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration2: p/2 = 143333 p = 286666 y = 4482640 x = 22352327
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)98259936138225 - (Int((x * x) / m)4303361 * m)98259928417939 = 7720286
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration3: p/2 = 71666.5 p = 143333 y = 4482640 x = 9912615
3/30/2012 9:26:46 AM	gis.asp: EX		post inner do: p = 143333 y = 4482640 x = 9912615
3/30/2012 9:26:46 AM	gis.asp: EX		outer iteration5: p = 143332 y = 11484547 x = 9912615
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)59602815921796 - (Int((x * x) / m)2610346 * m)59602810711454 = 5210342
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration1: p/2 = 35833 p = 71666 y = 11484547 x = 7720286
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)27147663756964 - (Int((x * x) / m)1188950 * m)27147650846050 = 12910914
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration2: p/2 = 17916.5 p = 35833 y = 11484547 x = 5210342
3/30/2012 9:26:46 AM	gis.asp: EX		post inner do: p = 35833 y = 11484547 x = 5210342
3/30/2012 9:26:46 AM	gis.asp: EX		outer iteration6: p = 35832 y = 12894538 x = 5210342
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)166691700315396 - (Int((x * x) / m)7300377 * m)166691690853723 = 9461673
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration1: p/2 = 8958 p = 17916 y = 12894538 x = 12910914
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)89523255958929 - (Int((x * x) / m)3920732 * m)89523246054868 = 9904061
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration2: p/2 = 4479 p = 8958 y = 12894538 x = 9461673
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)98090424291721 - (Int((x * x) / m)4295937 * m)98090414006163 = 10285558
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration3: p/2 = 2239.5 p = 4479 y = 12894538 x = 9904061
3/30/2012 9:26:46 AM	gis.asp: EX		post inner do: p = 4479 y = 12894538 x = 9904061
3/30/2012 9:26:46 AM	gis.asp: EX		outer iteration7: p = 4478 y = 5614290 x = 9904061
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)105792703371364 - (Int((x * x) / m)4633264 * m)105792702257936 = 1113428
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration1: p/2 = 1119.5 p = 2239 y = 5614290 x = 10285558
3/30/2012 9:26:46 AM	gis.asp: EX		post inner do: p = 2239 y = 5614290 x = 10285558
3/30/2012 9:26:46 AM	gis.asp: EX		outer iteration8: p = 2238 y = 7253850 x = 10285558
3/30/2012 9:26:46 AM	gis.asp: EX			(x * x)1239721911184 - (Int((x * x) / m)54294 * m)1239711135906 = 10775278
3/30/2012 9:26:46 AM	gis.asp: EX			inner iteration1: p/2 = 559.5 p = 1119 y = 7253850 x = 1113428
3/30/2012 9:26:46 AM	gis.asp: EX		post inner do: p = 1119 y = 7253850 x = 1113428
3/30/2012 9:26:47 AM	gis.asp: EX		outer iteration9: p = 1118 y = 22342221 x = 1113428
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)116106615977284 - (Int((x * x) / m)5084968 * m)116106594749432 = 21227852
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration1: p/2 = 279.5 p = 559 y = 22342221 x = 10775278
3/30/2012 9:26:47 AM	gis.asp: EX		post inner do: p = 559 y = 22342221 x = 10775278
3/30/2012 9:26:47 AM	gis.asp: EX		outer iteration10: p = 558 y = 907071 x = 10775278
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)450621700533904 - (Int((x * x) / m)19735286 * m)450621686088514 = 14445390
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration1: p/2 = 139.5 p = 279 y = 907071 x = 21227852
3/30/2012 9:26:47 AM	gis.asp: EX		post inner do: p = 279 y = 907071 x = 21227852
3/30/2012 9:26:47 AM	gis.asp: EX		outer iteration11: p = 278 y = 7727885 x = 21227852
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)208669292252100 - (Int((x * x) / m)9138814 * m)208669272567386 = 19684714
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration1: p/2 = 69.5 p = 139 y = 7727885 x = 14445390
3/30/2012 9:26:47 AM	gis.asp: EX		post inner do: p = 139 y = 7727885 x = 14445390
3/30/2012 9:26:47 AM	gis.asp: EX		outer iteration12: p = 138 y = 17056263 x = 14445390
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)387487965261796 - (Int((x * x) / m)16970301 * m)387487956852999 = 8408797
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration1: p/2 = 34.5 p = 69 y = 17056263 x = 19684714
3/30/2012 9:26:47 AM	gis.asp: EX		post inner do: p = 69 y = 17056263 x = 19684714
3/30/2012 9:26:47 AM	gis.asp: EX		outer iteration13: p = 68 y = 3411381 x = 19684714
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)70707866987209 - (Int((x * x) / m)3096699 * m)70707854180001 = 12807208
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration1: p/2 = 17 p = 34 y = 3411381 x = 8408797
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)164024576755264 - (Int((x * x) / m)7183568 * m)164024556030832 = 20724432
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration2: p/2 = 8.5 p = 17 y = 3411381 x = 12807208
3/30/2012 9:26:47 AM	gis.asp: EX		post inner do: p = 17 y = 3411381 x = 12807208
3/30/2012 9:26:47 AM	gis.asp: EX		outer iteration14: p = 16 y = 4229193 x = 12807208
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)429502081722624 - (Int((x * x) / m)18810338 * m)429502071845062 = 9877562
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration1: p/2 = 4 p = 8 y = 4229193 x = 20724432
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)97566231063844 - (Int((x * x) / m)4272980 * m)97566229961020 = 1102824
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration2: p/2 = 2 p = 4 y = 4229193 x = 9877562
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)1216220774976 - (Int((x * x) / m)53265 * m)1216215671235 = 5103741
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration3: p/2 = 1 p = 2 y = 4229193 x = 1102824
3/30/2012 9:26:47 AM	gis.asp: EX			(x * x)26048172195081 - (Int((x * x) / m)1140797 * m)26048158999303 = 13195778
3/30/2012 9:26:47 AM	gis.asp: EX			inner iteration4: p/2 = 0.5 p = 1 y = 4229193 x = 5103741
3/30/2012 9:26:47 AM	gis.asp: EX		post inner do: p = 1 y = 4229193 x = 5103741
3/30/2012 9:26:47 AM	gis.asp: EX		outer iteration15: p = 0 y = 230 x = 5103741
3/30/2012 9:26:47 AM	gis.asp: EX	post outer do: p = 0 y = 230 x = 5103741

Open in new window

0
Comment
Question by:dgerler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 29

Assisted Solution

by:sammySeltzer
sammySeltzer earned 400 total points
ID: 37787928
public object EX(ref System.Object x, System.Object p, System.Object m)
{
	y = 1;
	while (p > 0) {
		while ((p / 2) == Conversion.Int((p / 2))) {
			x = (x * x) - (Conversion.Int((x * x) / m) * m);
			p = p / 2;
		}
		y = (x * y) - (Conversion.Int((x * y) / m) * m);
		p = p - 1;
	}
	return y;
}

Open in new window


http://converter.telerik.com/
0
 

Author Comment

by:dgerler
ID: 37788075
That doesn't compile. Math can't be performed on the objects.

I have modified it to:

        public int Ex(ref int x, int p, int m)
        {
            int y = 1;
            while (p > 0)
            {
                while ((p / 2) == Convert.ToInt32(p/2))
                {
                    x = (x * x) - (Convert.ToInt32((x * x) / m) * m);
                    p = p / 2;
                }
                y = (x * y) - (Convert.ToInt32((x * y) / m) * m);
                p = p - 1;
            }
            return y;
        }

Open in new window


It still doesn't work correctly.
0
 

Accepted Solution

by:
dgerler earned 0 total points
ID: 37788896
It's fixed.

here's the functioning code..

x*x was overflowing the the int32 datatype and causing the number to go negative.

        public int Ex(ref Int64 x, Int64 p, Int64 m)
        {
            Int64 y = 1;
            while (p > 0)
            {
                while ((p % 2) == 0)
                {
                    x = (x * x) - (Convert.ToInt64((x * x) / m) * m);
                    p = p / 2;
                }
                y = (x * y) - (Convert.ToInt64((x * y) / m) * m);
                p = p - 1;
            }
            return (int)y;
        }

Open in new window

0
 

Author Closing Comment

by:dgerler
ID: 37805191
While SammySelzer provided some code it was not complete and require much more work to fix it. The main thing I got from it was changing the do..while to a while
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question